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

export interface Trailer {
    trailer_number: string;
    trailer_id?: number;
    ace_trailer_number?: string;
    trailer_type_key?: string;
    seal?: string;
    entity_code?: string;
    plate_number?: string;
    state?: string;
    country?: string;
    container_key?: string;
    is_active?: boolean;
    company_id?: number | string;
    tenant_id?: number | string;
}

export interface TrailerResponse {
    items: Trailer[];
    total: number;
    page: number;
    page_size: number;
}

class TrailersApi {
    private baseUrl = '/v1/a76/transportation/trailers';

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

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

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

    async update(id: string, data: Trailer, companyId: string | number): Promise<ApiResponse<Trailer>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.put<Trailer>(`${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 trailersApi = new TrailersApi();
