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

/** List item shape returned by GET /v1/a76/us-tariff-fractions/ (SITAR-backed, read-only). */
export interface USTariffFraction {
	id: number;
	code: string;
	fraction?: string | null;
	prefix?: string | null;
	type_code?: string | null;
	ad_valorem?: number | null;
	fixed_cost?: number | null;
	unit_of_measure?: string | null;
	description?: string | null;
	created_at?: string;
	updated_at?: string;
}

export interface USTariffFractionListResponse {
	items: USTariffFraction[];
	total: number;
	page: number;
	page_size: number;
	pages: number;
}

/** Paginated list from SITAR (same data as tariff-fractions?catalog=american). */
export async function getUSTariffFractions(
	page = 1,
	pageSize = 50,
	companyId: number,
	filters: Record<string, string | number | boolean> = {}
): Promise<ApiResponse<USTariffFractionListResponse>> {
	const queryParams = new URLSearchParams({
		page: page.toString(),
		page_size: pageSize.toString(),
		company_id: companyId.toString(),
		...Object.fromEntries(
			Object.entries(filters).map(([k, v]) => [k, String(v)])
		)
	});
	return await api.get(`/v1/a76/us-tariff-fractions/?${queryParams.toString()}`);
}
