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

// ========================
// ITEMS (pool global)
// ========================

export interface EquivalencyItem {
    id: number;
    original_field: string;
    external_field: string;
    conversion_factor: number | null;
    tenant_id: number;
    company_id: number;
}

export interface EquivalencyItemCreate {
    original_field: string;
    external_field: string;
    conversion_factor?: number | null;
}

export interface EquivalencyItemUpdate {
    original_field?: string;
    external_field?: string;
    conversion_factor?: number | null;
}

export interface EquivalencyItemListResponse {
    items: EquivalencyItem[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getEquivalencyItems(
    page: number = 1,
    pageSize: number = 50,
    companyId: number,
    filters: Record<string, any> = {}
): Promise<ApiResponse<EquivalencyItemListResponse>> {
    const params = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        company_id: companyId.toString(),
        ...filters
    });
    return await api.get(`/v1/a76/equivalencies/items/?${params.toString()}`);
}

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

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

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

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

// ========================
// CATÁLOGO (referencia item_id)
// ========================

export interface Equivalency {
    id: number;
    identifier: string;
    description: string | null;
    item_id: number | null;
    item: EquivalencyItem | null;
    tenant_id: number;
    company_id: number;
}

export interface EquivalencyCreate {
    identifier: string;
    description?: string | null;
    item_id?: number | null;
}

export interface EquivalencyUpdate {
    identifier?: string;
    description?: string | null;
    item_id?: number | null;
}

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}`);
}
