import { api } from '$lib/api';
import type { ApiResponse } 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<ApiResponse<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(`/v1/a76/electronic-notices/?${params.toString()}`);
    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(`/v1/a76/electronic-notices/${id}?${params.toString()}`);
    return response.data;
}

export async function createElectronicNotice(data: ElectronicNoticeCreate, companyId: number): Promise<ElectronicNotice> {
    const response = await api.post(`/v1/a76/electronic-notices/?company_id=${companyId}`, data);
    return response.data;
}

export async function updateElectronicNotice(id: number, data: ElectronicNoticeUpdate, companyId: number): Promise<ElectronicNotice> {
    const response = await api.put(`/v1/a76/electronic-notices/${id}?company_id=${companyId}`, data);
    return response.data;
}

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