/**
 * API Client para Sectors (a76 — tenant-scoped)
 */
import { api } from '$lib/api';

export interface Sector {
	id: number;
	key: string;
	description: string;
	authorized: boolean;
	company_id: number;
	tenant_id: number;
	created_at?: string;
	updated_at?: string;
}

export interface SectorListResponse {
	items: Sector[];
	total: number;
	page: number;
	page_size: number;
}

export interface CreateSectorData {
	key: string;
	description: string;
	authorized: boolean;
}

export interface UpdateSectorData {
	key?: string;
	description?: string;
	authorized?: boolean;
}

export const sectorsApi = {
	list: (page = 1, pageSize = 50, companyId: number, search?: string) => {
		const params = new URLSearchParams({
			page: page.toString(),
			page_size: pageSize.toString(),
			company_id: companyId.toString()
		});
		if (search) params.append('search', search);
		return api.get<SectorListResponse>(`/v1/a76/sectors/?${params.toString()}`);
	},

	get: (id: number, companyId: number) =>
		api.get<Sector>(`/v1/a76/sectors/${id}/?company_id=${companyId}`),

	getByKey: (key: string, companyId: number) => {
		const params = new URLSearchParams({
			page: '1',
			page_size: '1',
			company_id: companyId.toString(),
			key
		});
		return api.get<SectorListResponse>(`/v1/a76/sectors/?${params.toString()}`);
	},

	create: (data: CreateSectorData, companyId: number) =>
		api.post<Sector>(`/v1/a76/sectors/?company_id=${companyId}`, data),

	update: (id: number, data: UpdateSectorData, companyId: number) =>
		api.put<Sector>(`/v1/a76/sectors/${id}/?company_id=${companyId}`, data),

	delete: (id: number, companyId: number) =>
		api.delete(`/v1/a76/sectors/${id}/?company_id=${companyId}`)
};
