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

export interface DocumentTypeDigitization {
	id: number;
	code: string;
	description: string;
	active: boolean;
}

export interface DocumentTypeDigitizationListResponse {
    items: DocumentTypeDigitization[];
    total: number;
    page: number;
    page_size: number;
}

const BASE_URL = '/v1/a76/document-types-digitization';

/**
 * API de solo lectura para Tipos de Documentos de Digitalización
 */
export const documentTypesDigitizationApi = {
    list: (
        page = 1,
        pageSize = 50,
        companyId: number,
        search?: string,
        activeOnly = false
    ) => {
        const params = new URLSearchParams({
            page: page.toString(),
            page_size: pageSize.toString(),
            company_id: companyId.toString()
        });

        if (search) {
            params.append('search', search);
        }

        if (activeOnly) {
            params.append('active_only', 'true');
        }

        return api.get<DocumentTypeDigitizationListResponse>(`${BASE_URL}/?${params.toString()}`);
    },

    getAll: (companyId: number, activeOnly = true, search?: string) => {
        return documentTypesDigitizationApi.list(1, 2000, companyId, search, activeOnly);
    },

    getById: (id: number, companyId: number) => {
        return api.get<DocumentTypeDigitization>(`${BASE_URL}/${id}/?company_id=${companyId}`);
    },

    getByCode: (code: string, companyId: number) => {
        return api.get<DocumentTypeDigitization>(`${BASE_URL}/by-code/${code}/?company_id=${companyId}`);
    }
};