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

export interface FaData {
    origin_country?: string | null;
    sector?: string | null;
    fraction_type?: string | null;
}

export interface InvData {
    part_type?: string | null;
    material_type?: string | null;
    reference_number?: string | null;
    flex_reference_number?: string | null;
    equivalent_uom?: string | null;
    conversion_factor?: number | null;
    stock_uom?: string | null;
    alternate_uom?: string | null;
    conversion_uom?: string | null;
    added_value?: number | null;
    added_value_type?: string | null;
    assigned_client?: string | null;
    supplier_code?: string | null;
    is_textile?: string | null;
    bom_version?: number | null;
    is_repair?: string | null;
    is_hazardous?: string | null;
    emergency_number?: string | null;
    danger_class?: string | null;
    packaging_group?: string | null;
    width?: string | null;
    thickness?: string | null;
    specification?: string | null;
    total_value?: number | null;
    direct_labor?: number | null;
    general_expenses?: number | null;
    total_expenses?: number | null;
    depreciation?: number | null;
    tooling?: number | null;
    material_consumed?: number | null;
    profit?: number | null;
    us_fraction_alt?: string | null;
    ca_fraction?: string | null;
    ad_valorem_us?: number | null;
    nafta_result?: string | null;
    nafta_percentage?: number | null;
    dta?: string | null;
    dtb?: string | null;
    dtg?: string | null;

    usmca_fraction?: string | null;
    scrap_part_number?: string | null;
    waste_part_number?: string | null;
    scrap_description_en?: string | null;
    scrap_description_es?: string | null;
    scrap_export_fraction?: string | null;
    scrap_us_fraction?: string | null;
    equivalent_uom_2?: string | null;
    conversion_factor_2?: number | null;
    has_auxiliary?: boolean | null;
    auxiliary_uom?: string | null;
    auxiliary_conversion?: number | null;
    auxiliary_unit_cost?: number | null;
    mex_packing?: string | null;
    sales_order?: string | null;
    use_rule_8?: boolean | null;

    fraction_9801?: string | null;
    immex_type?: string | null;
    disable_movements?: boolean | null;
    pga_program_code?: string | null;
    non_discharge_clients?: unknown[] | null;
    bom_items?: unknown[] | null;
    countries?: unknown[] | null;
    fraction_type?: string | null;
    use_alternate_quantity?: boolean | null;
    repair_unit_cost?: number | null;
    repair_added_value?: number | null;
    sector?: string | null;
    origin_country?: string | null;
    supplier?: string | null;
    hazard_notes?: string | null;
    shipping_name?: string | null;
    un_number?: string | null;
    complementary_part?: string | null;
    preference_part?: string | null;
    substitute_part?: string | null;
}


export interface Part {
    id: number;
    tenant_id: number;
    company_id: number;

    // Identificación
    part_number: string;
    commercial_part_number: string | null;

    // Descripciones y Clase
    description_spanish: string | null;
    description_english: string | null;
    part_class: string | null;
    unit_of_measure: string | null;

    // Costos y Pesos
    unit_cost: number | null;
    currency_key: string | null;
    currency_type: string | null;
    unit_weight: number | null;
    weight_type: string | null;

    // Regulatorio
    fraction: string | null;
    us_fraction: string | null;
    fda_key: string | null;
    fcc_key: string | null;
    license_code: string | null;
    eccn: string | null;
    export_code: string | null;
    exclusion_symbol: string | null;

    // Estado y Media
    is_active: boolean;
    part_photo: string | null;
    created_at: string;
    updated_at: string;


    fa_data?: FaData | null;
    inv_data?: InvData | null;
}


export interface PartCreate extends Omit<Part, 'id' | 'tenant_id' | 'created_at' | 'updated_at'> {
}


export interface PartUpdate extends Partial<PartCreate> { }


export interface PartListResponse {
    items: Part[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export const partsApi = {

    list: (params: {
        company_id: number;
        page?: number;
        page_size?: number;
        q?: string;
        sort_by?: string;
        sort_order?: 'asc' | 'desc';
    }) => {
        const { company_id, page = 1, page_size = 50, q = '' } = params;

        const queryParams: Record<string, string> = {
            company_id: company_id.toString(),
            page: page.toString(),
            page_size: page_size.toString(),
        };

        if (q) {
            queryParams.description = q;
        }

        if (params.sort_by) queryParams.sort_by = params.sort_by;
        if (params.sort_order) queryParams.sort_order = params.sort_order;

        const query = new URLSearchParams(queryParams);

        return api.get<PartListResponse>(`/v1/a76/parts/?${query.toString()}`);
    },

    get: (id: number, company_id: number) => {
        return api.get<Part>(`/v1/a76/parts/${id}?company_id=${company_id}`);
    },

    create: (data: PartCreate, company_id: number) => {
        return api.post<Part>(`/v1/a76/parts/?company_id=${company_id}`, data);
    },

    update: (id: number, data: PartUpdate, company_id: number) => {
        return api.put<Part>(`/v1/a76/parts/${id}/?company_id=${company_id}`, data);
    },


    delete: (id: number, company_id: number) => {
        return api.delete<void>(`/v1/a76/parts/${id}?company_id=${company_id}`);
    }
};