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

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

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

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

export interface UMACEListResponse {
    items: UMACE[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

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

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

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

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

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