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

export interface Equivalency {
    id: number;
    fraccion_mex: string;
    fraccion_us: string;
    description?: string;
    tenant_id: number;
    company_id: number;
    created_at: string;
    updated_at?: string;
}

export interface EquivalencyCreate {
    fraccion_mex: string;
    fraccion_us: string;
    description?: string;
}

export interface EquivalencyUpdate {
    fraccion_mex?: string;
    fraccion_us?: string;
    description?: string;
}

export interface EquivalencyListResponse {
    items: Equivalency[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getEquivalencies(
    page: number = 1,
    pageSize: number = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<EquivalencyListResponse>> {
    const params = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });

    return await api.get(`/v1/a76/equivalencies/?${params.toString()}`);
}

export async function getEquivalency(id: number, companyId: number): Promise<ApiResponse<Equivalency>> {
    return await api.get(`/v1/a76/equivalencies/${id}/?company_id=${companyId}`);
}

export async function createEquivalency(
    data: EquivalencyCreate, 
    companyId: number
): Promise<ApiResponse<Equivalency>> {
    return await api.post(`/v1/a76/equivalencies/?company_id=${companyId}`, data);
}

export async function updateEquivalency(
    id: number, 
    data: EquivalencyUpdate, 
    companyId: number
): Promise<ApiResponse<Equivalency>> {
    return await api.put(`/v1/a76/equivalencies/${id}/?company_id=${companyId}`, data);
}

export async function deleteEquivalency(id: number, companyId: number): Promise<ApiResponse<void>> {
    return await api.delete(`/v1/a76/equivalencies/${id}/?company_id=${companyId}`);
}
