/**
 * API Client para UM American - Unidades de medida americanas
 */
import { api } from '$lib/api';
import type { ApiResponse } from '$lib/api';

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

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

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

export interface UMCustomsAmeListResponse {
    items: UMCustomsAme[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

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

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

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

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

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