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

export interface ElectronicNotice {
    id: number;
    // Campos del Modelo Python
    notice_number?: string;
    year?: string;
    patent?: string;
    pedimento?: string;
    file_sent?: string;
    file_response?: string;
    status?: string;
    invoice?: string;
    validation_acknowledgment?: string;
    fea?: string;
    certificate_number?: string;
    
    // Mixins
    tenant_id: number;
    company_id: number;
    created_at: string;
    updated_at?: string;
}

export interface ElectronicNoticeCreate {
    notice_number?: string;
    year?: string;
    patent?: string;
    pedimento?: string;
    file_sent?: string;
    file_response?: string;
    status?: string;
    invoice?: string;
    validation_acknowledgment?: string;
    fea?: string;
    certificate_number?: string;
}

export interface ElectronicNoticeUpdate extends Partial<ElectronicNoticeCreate> {}

export interface ElectronicNoticeListResponse {
    items: ElectronicNotice[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

export async function getElectronicNotices(
    page: number = 1,
    pageSize: number = 50,
    filters: Record<string, any> = {},
    companyId?: number
): Promise<ElectronicNoticeListResponse> {
    const params = new URLSearchParams({
        page: page.toString(),
        page_size: pageSize.toString(),
        ...filters
    });
    if (companyId) {
        params.append('company_id', companyId.toString());
    }

    const response = await api.get<ElectronicNoticeListResponse>(`/v1/a76/electronic-notices/?${params.toString()}`);
    if (response.error) {
        throw new Error(response.error);
    }
    if (!response.data) {
        throw new Error('No se pudieron obtener los avisos electrónicos');
    }
    return response.data;
}

export async function getElectronicNotice(id: number, companyId?: number): Promise<ElectronicNotice> {
    const params = new URLSearchParams();
    if (companyId) {
        params.append('company_id', companyId.toString());
    }
    const response = await api.get<ElectronicNotice>(`/v1/a76/electronic-notices/${id}?${params.toString()}`);
    if (response.error) {
        throw new Error(response.error);
    }
    if (!response.data) {
        throw new Error('No se pudo obtener el aviso electrónico');
    }
    return response.data;
}

export async function createElectronicNotice(data: ElectronicNoticeCreate, companyId: number): Promise<ElectronicNotice> {
    const response = await api.post<ElectronicNotice>(`/v1/a76/electronic-notices/?company_id=${companyId}`, data);
    if (response.error) {
        throw new Error(response.error);
    }
    if (!response.data) {
        throw new Error('No se pudo crear el aviso electrónico');
    }
    return response.data;
}

export async function updateElectronicNotice(id: number, data: ElectronicNoticeUpdate, companyId: number): Promise<ElectronicNotice> {
    const response = await api.put<ElectronicNotice>(`/v1/a76/electronic-notices/${id}/?company_id=${companyId}`, data);
    if (response.error) {
        throw new Error(response.error);
    }
    if (!response.data) {
        throw new Error('No se pudo actualizar el aviso electrónico');
    }
    return response.data;
}

export async function deleteElectronicNotice(id: number, companyId: number): Promise<void> {
    const response = await api.delete(`/v1/a76/electronic-notices/${id}?company_id=${companyId}`);
    if (response.error) {
        throw new Error(response.error);
    }
}

export const electronicNoticesApi = {
    list: (companyId: number, options: Record<string, any> = {}) => {
        const { page = '1', page_size = '50', ...filters } = options;
        return getElectronicNotices(Number(page), Number(page_size), filters, companyId) as any;
    },
    get: (id: number, companyId: number) => getElectronicNotice(id, companyId),
    create: (data: ElectronicNoticeCreate, companyId: number) => createElectronicNotice(data, companyId),
    update: (id: number, data: ElectronicNoticeUpdate, companyId: number) => updateElectronicNotice(id, data, companyId),
    delete: (id: number, companyId: number) => deleteElectronicNotice(id, companyId)
};