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

export interface ClassificationConcept {
    id: number;
    classification: string;
    description?: string;
    tenant_id: number;
    company_id: number;
    created_at: string;
    updated_at?: string;
}

export interface ClassificationConceptCreate {
    classification: string;
    description?: string;
}

export interface ClassificationConceptUpdate extends Partial<ClassificationConceptCreate> {}

export interface ClassificationConceptListResponse {
    items: ClassificationConcept[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

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

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

export async function getClassificationConcept(id: number, companyId: number): Promise<ApiResponse<ClassificationConcept>> {
    return await api.get(`/v1/a76/classification-concepts/${id}?company_id=${companyId}`);
}

export async function createClassificationConcept(
    data: ClassificationConceptCreate, 
    companyId: number
): Promise<ApiResponse<ClassificationConcept>> {
    return await api.post(`/v1/a76/classification-concepts/?company_id=${companyId}`, data);
}

export async function updateClassificationConcept(
    id: number, 
    data: ClassificationConceptUpdate,
    companyId: number
): Promise<ApiResponse<ClassificationConcept>> {
    return await api.put(`/v1/a76/classification-concepts/${id}/?company_id=${companyId}`, data);
}

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

export const classificationApi = {
    list: (companyId: number, options: Record<string, any> = {}) => {
        const { page = '1', page_size = '50', ...filters } = options;
        return getClassificationConcepts(Number(page), Number(page_size), companyId, filters);
    },
    get: (id: number, companyId: number) => getClassificationConcept(id, companyId),
    create: (data: ClassificationConceptCreate, companyId: number) => createClassificationConcept(data, companyId),
    update: (id: number, data: ClassificationConceptUpdate, companyId: number) => updateClassificationConcept(id, data, companyId),
    delete: (id: number, companyId: number) => deleteClassificationConcept(id, companyId)
};