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

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

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

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

export interface UMOMAListResponse {
    items: UMOMA[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

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

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

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

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

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