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

export interface Manifest {
    id: number;
    manifest_number?: string;
    importer_details?: string;
    person_in_charge?: string;
    consigned_to?: string;
    sent_by?: string;
    foreign_exit_port?: string;
    foreign_exit_port_loc?: string;
    destination_port?: string;
    destination_port_loc?: string;
    entry_port?: string;
    entry_port_loc?: string;
    entry_date?: number;
    net_weight?: number;
    gross_weight?: number;
    total_value?: number;
    description?: string;
    broker_code?: string;
    carrier_code?: string;
    payment_invoice_number?: string;
    seal_number?: string;
    entry_hour?: number;
    hazardous_material?: string;
    transport_mode?: string;
    transport_code?: string;
    trailer_number?: string;
    status?: string;
    status_description?: string;
    manifest_type?: string;
    created_at?: string;
    updated_at?: string;
    company_id?: number;
    tenant_id?: string;
}

export interface ManifestResponse {
    items: Manifest[];
    total: number;
    page: number;
    page_size: number;
}

export type ManifestCreate = Omit<Manifest, 'id' | 'created_at' | 'updated_at'>;
export type ManifestUpdate = Partial<ManifestCreate>;

class ManifestApi {
    private baseUrl = '/v1/a76/manifests';

    async list(
        companyId: string | number,
        filters?: Record<string, any>
    ): Promise<ApiResponse<ManifestResponse>> {
        const params = new URLSearchParams({
            company_id: companyId.toString()
        });

        if (filters) {
            Object.entries(filters).forEach(([key, value]) => {
                if (value !== undefined && value !== null && value !== '') {
                    params.append(key, value.toString());
                }
            });
        }

        return api.get<ManifestResponse>(`${this.baseUrl}?${params.toString()}`);
    }

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

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

    async update(
        companyId: string | number,
        id: number,
        manifest: ManifestUpdate
    ): Promise<ApiResponse<Manifest>> {
        const params = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.patch<Manifest>(`${this.baseUrl}/${id}?${params.toString()}`, manifest);
    }

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

    async updateInvoiceCompliance(
        invoiceId: number,
        companyId: string | number,
        manifestNumber: string | null
    ): Promise<ApiResponse<boolean>> {
        const params = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.patch<boolean>(`${this.baseUrl}/invoices/${invoiceId}/compliance?${params.toString()}`, {
            manifest_number: manifestNumber
        });
    }
}

export const manifestApi = new ManifestApi();
