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

// --- ACE ---
export interface UnitOfMeasureACE {
    id: number;
    code: string;
    description: string | null;
    created_at: string | null;
    updated_at: string | null;
}

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

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

export interface UnitOfMeasureACEListResponse {
    items: UnitOfMeasureACE[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getUnitsOfMeasureACE(
    page = 1,
    pageSize = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UnitOfMeasureACEListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    return await api.get(`/v1/a76/units-of-measure/ace/?${queryParams.toString()}`);
}

export async function createUnitOfMeasureACE(data: UnitOfMeasureACECreate, companyId: number): Promise<ApiResponse<UnitOfMeasureACE>> {
    return await api.post(`/v1/a76/units-of-measure/ace/?company_id=${companyId}`, data);
}

export async function updateUnitOfMeasureACE(id: number, data: UnitOfMeasureACEUpdate, companyId: number): Promise<ApiResponse<UnitOfMeasureACE>> {
    return await api.put(`/v1/a76/units-of-measure/ace/${id}/?company_id=${companyId}`, data);
}

export async function deleteUnitOfMeasureACE(id: number, companyId: number): Promise<ApiResponse<void>> {
    return await api.delete(`/v1/a76/units-of-measure/ace/${id}?company_id=${companyId}`);
}

// --- OMA ---
export interface UnitOfMeasureOMA {
    id: number;
    code: string;
    description: string | null;
    created_at: string | null;
    updated_at: string | null;
}

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

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

export interface UnitOfMeasureOMAListResponse {
    items: UnitOfMeasureOMA[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getUnitsOfMeasureOMA(
    page: number = 1,
    pageSize: number = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UnitOfMeasureOMAListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    return await api.get(`/v1/a76/units-of-measure/oma/?${queryParams.toString()}`);
}

export async function createUnitOfMeasureOMA(data: UnitOfMeasureOMACreate, companyId: number): Promise<ApiResponse<UnitOfMeasureOMA>> {
    return await api.post(`/v1/a76/units-of-measure/oma/?company_id=${companyId}`, data);
}

export async function updateUnitOfMeasureOMA(id: number, data: UnitOfMeasureOMAUpdate, companyId: number): Promise<ApiResponse<UnitOfMeasureOMA>> {
    return await api.put(`/v1/a76/units-of-measure/oma/${id}/?company_id=${companyId}`, data);
}

export async function deleteUnitOfMeasureOMA(id: number, companyId: number): Promise<ApiResponse<void>> {
    return await api.delete(`/v1/a76/units-of-measure/oma/${id}?company_id=${companyId}`);
}

// --- American ---
export interface UnitOfMeasureAmerican {
    id: number;
    code: string;
    description: string | null;
    created_at: string | null;
    updated_at: string | null;
}

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

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

export interface UnitOfMeasureAmericanListResponse {
    items: UnitOfMeasureAmerican[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getUnitsOfMeasureAmerican(
    page: number = 1,
    pageSize: number = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UnitOfMeasureAmericanListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    return await api.get(`/v1/a76/units-of-measure/american/?${queryParams.toString()}`);
}

export async function createUnitOfMeasureAmerican(data: UnitOfMeasureAmericanCreate, companyId: number): Promise<ApiResponse<UnitOfMeasureAmerican>> {
    return await api.post(`/v1/a76/units-of-measure/american/?company_id=${companyId}`, data);
}

export async function updateUnitOfMeasureAmerican(id: number, data: UnitOfMeasureAmericanUpdate, companyId: number): Promise<ApiResponse<UnitOfMeasureAmerican>> {
    return await api.put(`/v1/a76/units-of-measure/american/${id}/?company_id=${companyId}`, data);
}

export async function deleteUnitOfMeasureAmerican(id: number, companyId: number): Promise<ApiResponse<void>> {
    return await api.delete(`/v1/a76/units-of-measure/american/${id}?company_id=${companyId}`);
}

// --- General ---
export interface UnitOfMeasureGeneral {
    id: number;
    code: string;
    description: string | null;
    created_at: string | null;
    updated_at: string | null;
}

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

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

export interface UnitOfMeasureGeneralListResponse {
    items: UnitOfMeasureGeneral[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getUnitsOfMeasureGeneral(
    page = 1,
    pageSize = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UnitOfMeasureGeneralListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    return await api.get(`/v1/a76/units-of-measure/general/?${queryParams.toString()}`);
}

export async function createUnitOfMeasureGeneral(data: UnitOfMeasureGeneralCreate, companyId: number): Promise<ApiResponse<UnitOfMeasureGeneral>> {
    return await api.post(`/v1/a76/units-of-measure/general/?company_id=${companyId}`, data);
}

export async function updateUnitOfMeasureGeneral(id: number, data: UnitOfMeasureGeneralUpdate, companyId: number): Promise<ApiResponse<UnitOfMeasureGeneral>> {
    return await api.put(`/v1/a76/units-of-measure/general/${id}/?company_id=${companyId}`, data);
}

export async function deleteUnitOfMeasureGeneral(id: number, companyId: number): Promise<ApiResponse<void>> {
    return await api.delete(`/v1/a76/units-of-measure/general/${id}?company_id=${companyId}`);
}

// --- Customs ---
export interface UnitOfMeasureCustoms {
    id: number;
    code: string;
    description: string | null;
    a76_unit_code: string | null;
    created_at: string | null;
    updated_at: string | null;
}

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

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

export interface UnitOfMeasureCustomsListResponse {
    items: UnitOfMeasureCustoms[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getUnitsOfMeasureCustoms(
    page: number = 1,
    pageSize: number = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UnitOfMeasureCustomsListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    return await api.get(`/v1/a76/units-of-measure/customs/?${queryParams.toString()}`);
}

export async function createUnitOfMeasureCustoms(data: UnitOfMeasureCustomsCreate, companyId: number): Promise<ApiResponse<UnitOfMeasureCustoms>> {
    return await api.post(`/v1/a76/units-of-measure/customs/?company_id=${companyId}`, data);
}

export async function updateUnitOfMeasureCustoms(id: number, data: UnitOfMeasureCustomsUpdate, companyId: number): Promise<ApiResponse<UnitOfMeasureCustoms>> {
    return await api.put(`/v1/a76/units-of-measure/customs/${id}/?company_id=${companyId}`, data);
}

export async function deleteUnitOfMeasureCustoms(id: number, companyId: number): Promise<ApiResponse<void>> {
    return await api.delete(`/v1/a76/units-of-measure/customs/${id}?company_id=${companyId}`);
}

export interface UnitOfMeasure {
    id: number;
    code: string;           // Ej: KG, PZ
    description: string | null;
    description_en: string | null;
    customs_code: string | null;
    american_code: string | null;
    created_at: string | null;
    updated_at: string | null;
}

export interface UnitOfMeasureListResponse {
    items: UnitOfMeasure[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getUnitsOfMeasure(
    page: number = 1,
    pageSize: number = 1000,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<UnitOfMeasureListResponse>> {
    const queryParams = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    // Apunta a /v1/a76/units-of-measure/ (La ruta base del router)
    return await api.get(`/v1/a76/units-of-measure/?${queryParams.toString()}`);
}

export const aceUnitsApi = {
    list: (companyId: number, options: any = {}) => getUnitsOfMeasureACE(Number(options.page || 1), Number(options.page_size || 50), companyId, options),
    get: (id: number, companyId: number) => api.get(`/v1/a76/units-of-measure/ace/${id}/?company_id=${companyId}`),
    create: (data: any, companyId: number) => createUnitOfMeasureACE(data, companyId),
    update: (id: number, data: any, companyId: number) => updateUnitOfMeasureACE(id, data, companyId),
    delete: (id: number, companyId: number) => deleteUnitOfMeasureACE(id, companyId)
};

export const omaUnitsApi = {
    list: (companyId: number, options: any = {}) => getUnitsOfMeasureOMA(Number(options.page || 1), Number(options.page_size || 50), companyId, options),
    get: (id: number, companyId: number) => api.get(`/v1/a76/units-of-measure/oma/${id}/?company_id=${companyId}`),
    create: (data: any, companyId: number) => createUnitOfMeasureOMA(data, companyId),
    update: (id: number, data: any, companyId: number) => updateUnitOfMeasureOMA(id, data, companyId),
    delete: (id: number, companyId: number) => deleteUnitOfMeasureOMA(id, companyId)
};

export const americanUnitsApi = {
    list: (companyId: number, options: any = {}) => getUnitsOfMeasureAmerican(Number(options.page || 1), Number(options.page_size || 50), companyId, options),
    get: (id: number, companyId: number) => api.get(`/v1/a76/units-of-measure/american/${id}/?company_id=${companyId}`),
    create: (data: any, companyId: number) => createUnitOfMeasureAmerican(data, companyId),
    update: (id: number, data: any, companyId: number) => updateUnitOfMeasureAmerican(id, data, companyId),
    delete: (id: number, companyId: number) => deleteUnitOfMeasureAmerican(id, companyId)
};

export const generalUnitsApi = {
    list: (companyId: number, options: any = {}) => getUnitsOfMeasureGeneral(Number(options.page || 1), Number(options.page_size || 50), companyId, options),
    get: (id: number, companyId: number) => api.get(`/v1/a76/units-of-measure/general/${id}/?company_id=${companyId}`),
    create: (data: any, companyId: number) => createUnitOfMeasureGeneral(data, companyId),
    update: (id: number, data: any, companyId: number) => updateUnitOfMeasureGeneral(id, data, companyId),
    delete: (id: number, companyId: number) => deleteUnitOfMeasureGeneral(id, companyId)
};

export const customsUnitsApi = {
    list: (companyId: number, options: any = {}) => getUnitsOfMeasureCustoms(Number(options.page || 1), Number(options.page_size || 50), companyId, options),
    get: (id: number, companyId: number) => api.get(`/v1/a76/units-of-measure/customs/${id}/?company_id=${companyId}`),
    create: (data: any, companyId: number) => createUnitOfMeasureCustoms(data, companyId),
    update: (id: number, data: any, companyId: number) => updateUnitOfMeasureCustoms(id, data, companyId),
    delete: (id: number, companyId: number) => deleteUnitOfMeasureCustoms(id, companyId)
};

export const unitsOfMeasureApi = {
    list: (companyId: number, options: any = {}) => getUnitsOfMeasure(Number(options.page || 1), Number(options.page_size || 50), companyId, options),
    get: (id: number, companyId: number) => api.get(`/v1/a76/units-of-measure/${id}/?company_id=${companyId}`),
};