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

// --- Interfaces para detalles (Hijos) ---

export interface DodaContainerSeal {
    id: number;
    doda_sys_id: number;
    seal_line: number;
    seal_value?: string;
}

export interface DodaContainerSealCreate {
    seal_value?: string;
}

export interface DodaContainer {
    id: number;
    doda_sys_id: number;
    container_line: number;
    container_value?: string;
    seals?: string;
    seals_detail?: DodaContainerSeal[];
}

export interface DodaContainerCreate {
    container_value?: string;
    seals?: string;
    seals_detail?: DodaContainerSealCreate[];
}

export interface DodaAmericanPedimento {
    id: number;
    doda_sys_id: number;
    american_pedimento_line: number;
    american_pedimento_type?: string;
    american_pedimento_value?: string;
}

export interface DodaAmericanPedimentoCreate {
    american_pedimento_type?: string;
    american_pedimento_value?: string;
}

export interface DodaPedimento {
    id: number;
    doda_sys_id: number;
    pedimento_line: number;
    authorization_patent?: string;
    document?: string;
    shipment?: string;
    cove?: string;
    umc?: string;
    effective_amount_usd?: number;
    difference_amount_usd?: number;
    dta_niu?: string;
    article_7?: boolean;
    pedimento_sys_id?: number;
    invoice_line?: number;
    part_ii_line?: number;
    pedimento_type?: string;
    zero_packaging_validation?: boolean;
}

export interface DodaPedimentoCreate {
    authorization_patent?: string;
    document?: string;
    shipment?: string;
    effective_amount_usd?: number;
}

// --- Interfaces Principales (Padre) ---

export interface Doda {
    id: number;
    integration_number?: string;
    doda_date?: number;
    doda_time?: number;
    dispatch_customs?: string;
    customs_sections?: string;
    patent?: string;
    pedimentos?: string;
    caat?: string;
    transport_identification?: string;
    fast_id?: string;
    operation_type?: string;
    status?: string;

    // Relaciones
    containers?: DodaContainer[];
    american_pedimentos?: DodaAmericanPedimento[];
    pedimentos_detail?: DodaPedimento[];

    // Metadata
    tenant_id?: string;
    created_at?: string;
    updated_at?: string;

    // Campos extra del formulario que vimos en el frontend
    selected?: boolean;
    user_selected?: string;
    last_user?: string;
    responsible?: string;
    carrier?: string;
    shipments?: string;
    pedimento_type?: string;
    original_chain?: string;
    serial_number?: string;
    electronic_signature?: string;
    transaction_number?: string;
    linq_sat_qr?: string;
    sat_certificate?: string;
    sat_digital_seal?: string;
    xml_doda_sent_path?: string;
    xml_doda_response_path?: string;
    sat_original_chain?: string;
    customs_clearance?: number;
    unique_badge_number?: string;
}

export interface DodaCreate {
    integration_number?: string;
    doda_date?: number;
    doda_time?: number;
    dispatch_customs?: string;
    customs_sections?: string;
    patent?: string;
    pedimentos?: string;
    caat?: string;
    transport_identification?: string;
    fast_id?: string;
    operation_type?: string;
    selected?: boolean;
    user_selected?: string;
    last_user?: string;
    responsible?: string;
    carrier?: string;
    shipments?: string;
    pedimento_type?: string;
    original_chain?: string;
    serial_number?: string;
    electronic_signature?: string;
    transaction_number?: string;
    status?: string;
    linq_sat_qr?: string;
    sat_certificate?: string;
    sat_digital_seal?: string;
    xml_doda_sent_path?: string;
    xml_doda_response_path?: string;
    sat_original_chain?: string;
    customs_clearance?: number;
    unique_badge_number?: string;
}

export interface DodaUpdate extends Partial<DodaCreate> { }

export interface DodaListResponse {
    items: Doda[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

// --- Funciones de API ---

export async function getDodas(
    page: number = 1,
    pageSize: number = 50,
    filters: Record<string, any> = {},
    companyId?: number
): Promise<ApiResponse<DodaListResponse>> {
    const params = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        ...filters
    });
    if (companyId) {
        params.append('company_id', companyId.toString());
    }
    const response = await api.get(`/v1/a76/doda/?${params.toString()}`);
    return response.data;
}

export async function getDoda(id: number, companyId?: number): Promise<Doda> {
    const params = new URLSearchParams();
    if (companyId) {
        params.append('company_id', companyId.toString());
    }
    const response = await api.get(`/v1/a76/doda/${id}/detail?${params.toString()}`);
    return response.data;
}

export async function createDoda(data: DodaCreate, companyId: number): Promise<Doda> {
    const response = await api.post(`/v1/a76/doda/?company_id=${companyId}`, data);
    return response.data;
}

export async function updateDoda(id: number, data: DodaUpdate, companyId: number): Promise<Doda> {
    const response = await api.put(`/v1/a76/doda/${id}/?company_id=${companyId}`, data);
    return response.data;
}

export async function deleteDoda(id: number, companyId: number): Promise<void> {
    await api.delete(`/v1/a76/doda/${id}?company_id=${companyId}`);
}