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

// ==========================================
// ERROR CLASSIFICATION
// ==========================================

export interface ErrorClassification {
    id: number;
    code: string;
    level?: string;
    errors?: ErrorCatalog[];
    tenant_id?: string; 
    created_at?: string;
    updated_at?: string;
}

export interface ErrorClassificationCreate {
    code: string;
    level?: string;
}

export interface ErrorClassificationUpdate {
    level?: string;
}

export interface ErrorClassificationListResponse {
    items: ErrorClassification[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}


export interface ErrorCatalog {
    id: number;
    code: string;
    description?: string;
    classification_id?: number;
    classification?: ErrorClassification; 
    tenant_id?: string;
    created_at?: string;
    updated_at?: string;
}

export interface ErrorCatalogCreate {
    code: string;
    description?: string;
    classification_id?: number;
}

export interface ErrorCatalogUpdate {
    description?: string;
    classification_id?: number;
}

export interface ErrorCatalogListResponse {
    items: ErrorCatalog[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

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

    return await api.get(`/v1/a76/error-catalogs/classifications/?${params.toString()}`);
}

export async function getErrorClassification(id: number, companyId: number): Promise<ApiResponse<ErrorClassification>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.get(`/v1/a76/error-catalogs/classifications/${id}?${params.toString()}`);
}

export async function createErrorClassification(data: ErrorClassificationCreate, companyId: number): Promise<ApiResponse<ErrorClassification>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.post(`/v1/a76/error-catalogs/classifications/?${params.toString()}`, data);
}

export async function updateErrorClassification(id: number, data: ErrorClassificationUpdate, companyId: number): Promise<ApiResponse<ErrorClassification>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.put(`/v1/a76/error-catalogs/classifications/${id}/?${params.toString()}`, data);
}

export async function deleteErrorClassification(id: number, companyId: number): Promise<ApiResponse<void>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.delete(`/v1/a76/error-catalogs/classifications/${id}?${params.toString()}`);
}

// --- Catalogs ---

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

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

export async function getErrorCatalog(id: number, companyId: number): Promise<ApiResponse<ErrorCatalog>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.get(`/v1/a76/error-catalogs/${id}?${params.toString()}`);
}

export async function createErrorCatalog(data: ErrorCatalogCreate, companyId: number): Promise<ApiResponse<ErrorCatalog>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.post(`/v1/a76/error-catalogs/?${params.toString()}`, data);
}

export async function updateErrorCatalog(id: number, data: ErrorCatalogUpdate, companyId: number): Promise<ApiResponse<ErrorCatalog>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.put(`/v1/a76/error-catalogs/${id}/?${params.toString()}`, data);
}

export async function deleteErrorCatalog(id: number, companyId: number): Promise<ApiResponse<void>> {
    const params = new URLSearchParams({ company_id: companyId.toString() });
    return await api.delete(`/v1/a76/error-catalogs/${id}?${params.toString()}`);
}

export const errorCatalogsApi = {
    list: (companyId: number, options: any = {}) => getErrorCatalogs(companyId, Number(options.page || 1), Number(options.page_size || 50), options),
    get: (id: number, companyId: number) => getErrorCatalog(id, companyId),
    create: (data: any, companyId: number) => createErrorCatalog(data, companyId),
    update: (id: number, data: any, companyId: number) => updateErrorCatalog(id, data, companyId),
    delete: (id: number, companyId: number) => deleteErrorCatalog(id, companyId)
};