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

export interface HistoricalFraction {
    id: number;
    historical_fraction: string | null;
    unit_of_measure_code: string | null;
    country: string | null;
    fraction_type: string | null;
    sector: string | null;
    import_tax_rate: number | null;
    export_tax_rate: number | null;
    publication_date: string | null;
    is_immex: boolean | null;
    normal_temporality: boolean | null;
    services_temporality: boolean | null;
    certified_temporality: boolean | null;
    by_log: boolean | null;
    end_date: string | null;
}

export interface HistoricalFractionCreate {
    historical_fraction: string;
    unit_of_measure_code?: string | null;
    country?: string | null;
    fraction_type?: string | null;
    sector?: string | null;
    import_tax_rate?: number | null;
    export_tax_rate?: number | null;
    publication_date?: string | null;
    is_immex?: boolean | null;
    normal_temporality?: boolean | null;
    services_temporality?: boolean | null;
    certified_temporality?: boolean | null;
    by_log?: boolean | null;
    end_date?: string | null;
}

export interface HistoricalFractionUpdate {
    historical_fraction?: string;
    unit_of_measure_code?: string | null;
    country?: string | null;
    fraction_type?: string | null;
    sector?: string | null;
    import_tax_rate?: number | null;
    export_tax_rate?: number | null;
    publication_date?: string | null;
    is_immex?: boolean | null;
    normal_temporality?: boolean | null;
    services_temporality?: boolean | null;
    certified_temporality?: boolean | null;
    by_log?: boolean | null;
    end_date?: string | null;
}

export interface HistoricalFractionList {
    items: HistoricalFraction[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getHistoricalFractions(
    companyId: number,
    historicalFraction?: string,
    page = 1,
    pageSize = 50
): Promise<HistoricalFractionList> {
    const params = new URLSearchParams({
        company_id: companyId.toString(),
        page: page.toString(),
        page_size: pageSize.toString()
    });

    if (historicalFraction) params.append('historical_fraction', historicalFraction);

    const response = await api.get<HistoricalFractionList>(`/v1/a76/fractions/historical-tariff-fractions/?${params.toString()}`);
    if (!response.data) throw new Error('Error fetching historical fractions');
    return response.data;
}

export async function createHistoricalFraction(
    companyId: number,
    data: HistoricalFractionCreate
): Promise<ApiResponse<HistoricalFraction>> {
    return await api.post(`/v1/a76/fractions/historical-tariff-fractions/?company_id=${companyId}`, data);
}

export async function updateHistoricalFraction(
    companyId: number,
    id: number,
    data: HistoricalFractionUpdate
): Promise<ApiResponse<HistoricalFraction>> {
    return await api.put(`/v1/a76/fractions/historical-tariff-fractions/${id}/?company_id=${companyId}`, data);
}

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