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

// Interfaces
export interface USTariffFraction {
	id: number;
	code: string;
	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 | null;
	updated_at: string | null;
}

export interface USTariffFractionCreate {
	code: string;
	prefix?: string | null;
	type_code?: string | null;
	ad_valorem?: number | null;
	fixed_cost?: number | null;
	unit_of_measure?: string | null;
	description?: string | null;
}

export interface USTariffFractionUpdate {
	prefix?: string | null;
	type_code?: string | null;
	ad_valorem?: number | null;
	fixed_cost?: number | null;
	unit_of_measure?: string | null;
	description?: string | null;
}

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

// API Functions
export async function getUSTariffFractions(
	page = 1,
	pageSize = 50,
	companyId: number,
	filters: Record<string, any> = {}
): Promise<ApiResponse<USTariffFractionListResponse>> {
	const queryParams = new URLSearchParams({
		page: page.toString(),
		page_size: pageSize.toString(),
		company_id: companyId.toString(),
		...filters
	});
	return await api.get(`/v1/a76/us-tariff-fractions/?${queryParams.toString()}`);
}

export async function getUSTariffFractionById(
	id: number,
	companyId: number
): Promise<ApiResponse<USTariffFraction>> {
	return await api.get(`/v1/a76/us-tariff-fractions/${id}/?company_id=${companyId}`);
}

export async function createUSTariffFraction(
	data: USTariffFractionCreate,
	companyId: number
): Promise<ApiResponse<USTariffFraction>> {
	return await api.post(`/v1/a76/us-tariff-fractions/?company_id=${companyId}`, data);
}

export async function updateUSTariffFraction(
	id: number,
	data: USTariffFractionUpdate,
	companyId: number
): Promise<ApiResponse<USTariffFraction>> {
	return await api.put(`/v1/a76/us-tariff-fractions/${id}/?company_id=${companyId}`, data);
}

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