/**
 * API Client para UM Customs (Mexican) - Unidades de medida para aduanas mexicanas
 */
import { api } from '$lib/api';
import type { ApiResponse } from '$lib/api';

export interface UMCustomsMex {
    id: number;
    code: string;
    description: string | null;
    a76_unit_code: string | null;
    created_at: string | null;
    updated_at: string | null;
}

export interface UMCustomsMexCreate {
    code: string;
    description?: string | null;
    a76_unit_code?: string | null;
}

export interface UMCustomsMexUpdate {
    code?: string;
    description?: string | null;
    a76_unit_code?: string | null;
}

export interface UMCustomsMexListResponse {
    items: UMCustomsMex[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

/**
 * Lista todas las unidades de medida para aduanas mexicanas
 */
export async function getUMCustomsMex(
    page = 1,
    pageSize = 50,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UMCustomsMexListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        ...filters
    });
    return await api.get(`/a76/units-of-measure/customs/?${queryParams.toString()}`);
}

/**
 * Obtiene una unidad de medida por ID
 */
export async function getUMCustomsMexById(id: number): Promise<ApiResponse<UMCustomsMex>> {
    return await api.get(`/a76/units-of-measure/customs/${id}/`);
}

/**
 * Crea una nueva unidad de medida
 */
export async function createUMCustomsMex(data: UMCustomsMexCreate): Promise<ApiResponse<UMCustomsMex>> {
    return await api.post('/a76/units-of-measure/customs/', data);
}

/**
 * Actualiza una unidad de medida
 */
export async function updateUMCustomsMex(id: number, data: UMCustomsMexUpdate): Promise<ApiResponse<UMCustomsMex>> {
    return await api.put(`/a76/units-of-measure/customs/${id}/`, data);
}

/**
 * Elimina una unidad de medida
 */
export async function deleteUMCustomsMex(id: number): Promise<ApiResponse<void>> {
    return await api.delete(`/a76/units-of-measure/customs/${id}/`);
}
