import { api, type ApiResponse } from '$lib/api';

export interface Vehicle {
    vehicle_key: string;
    vehicle_id?: number;
    ace_vehicle_key?: string;
    transporter_key?: string;
    transport_identifier?: string;
    transport_type?: string;
    entity_code?: string;
    transponder_number?: string;
    dot_number?: string;
    plate_number?: string;
    city?: string;
    state?: string;
    country?: string;
    seal?: string;
    insurance_company_name?: string;
    insurance_number?: string;
    insurance_amount?: number;
    insurance_date?: number;
    box_number?: string;
    brand?: string;
    year?: string;
    series?: string;
    description?: string;
    engine_number?: string;
    sct_permission?: string;
    color?: string;
    container_key?: string;
    company_id?: number | string;
    tenant_id?: number | string;
}

export interface VehicleResponse {
    items: Vehicle[];
    total: number;
    page: number;
    page_size: number;
}

class VehiclesApi {
    private baseUrl = '/v1/a76/transportation/vehicles';

    async list(
        companyId: string | number,
        params?: Record<string, any>
    ): Promise<ApiResponse<VehicleResponse>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString(),
            ...params
        });
        return api.get<VehicleResponse>(`${this.baseUrl}?${queryParams.toString()}`);
    }

    async get(id: string, companyId: string | number): Promise<ApiResponse<Vehicle>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.get<Vehicle>(`${this.baseUrl}/${id}?${queryParams.toString()}`);
    }

    async create(data: Vehicle, companyId: string | number): Promise<ApiResponse<Vehicle>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.post<Vehicle>(`${this.baseUrl}?${queryParams.toString()}`, data);
    }

    async update(id: string, data: Vehicle, companyId: string | number): Promise<ApiResponse<Vehicle>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.put<Vehicle>(`${this.baseUrl}/${id}/?${queryParams.toString()}`, data);
    }

    async delete(id: string, companyId: string | number): Promise<ApiResponse<void>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.delete<void>(`${this.baseUrl}/${id}?${queryParams.toString()}`);
    }
}

export const vehiclesApi = new VehiclesApi();
