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

export interface PaginatedResponse {
	page: number;
	page_size: number;
	total: number;
	total_pages: number;
}

export interface MultiCurrencyType {
	id: number;
	currency_type_code: string;
	country_key: string | null;
	conversion_factor: number | null;
	publication_date: number;
	company_id: number;
	tenant_id: number;
}

export interface MultiCurrencyTypeCreate {
	currency_type_code: string;
	country_key?: string | null;
	conversion_factor?: number | null;
	publication_date: number;
}

export interface MultiCurrencyTypeUpdate {
	currency_type_code?: string;
	country_key?: string | null;
	conversion_factor?: number | null;
	publication_date?: number;
}

export interface MultiCurrencyTypeListResponse extends PaginatedResponse {
	items: MultiCurrencyType[];
}

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

export async function getMultiCurrencyTypes(
	companyId: number,
	page?: number,
	pageSize?: number,
	filters?: { currency_type_code?: string; country_key?: string }
): Promise<ApiResponse<MultiCurrencyTypeListResponse>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	if (page) params.append('page', page.toString());
	if (pageSize) params.append('page_size', pageSize.toString());
	if (filters?.currency_type_code) params.append('currency_type_code', filters.currency_type_code);
	if (filters?.country_key) params.append('country_key', filters.country_key);

	return api.get<MultiCurrencyTypeListResponse>(`/v1/a76/multi-currency-types/?${params.toString()}`);
}

export async function getMultiCurrencyType(
	multiCurrencyTypeId: number,
	companyId: number
): Promise<ApiResponse<MultiCurrencyType>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.get<MultiCurrencyType>(`/v1/a76/multi-currency-types/${multiCurrencyTypeId}?${params.toString()}`);
}

export async function createMultiCurrencyType(
	data: MultiCurrencyTypeCreate,
	companyId: number
): Promise<ApiResponse<MultiCurrencyType>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.post<MultiCurrencyType>(`/v1/a76/multi-currency-types/?${params.toString()}`, data);
}

export async function updateMultiCurrencyType(
	multiCurrencyTypeId: number,
	data: MultiCurrencyTypeUpdate,
	companyId: number
): Promise<ApiResponse<MultiCurrencyType>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.put<MultiCurrencyType>(
		`/v1/a76/multi-currency-types/${multiCurrencyTypeId}/?${params.toString()}`,
		data
	);
}

export async function deleteMultiCurrencyType(
	multiCurrencyTypeId: number,
	companyId: number
): Promise<ApiResponse<any>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.delete(`/v1/a76/multi-currency-types/${multiCurrencyTypeId}?${params.toString()}`);
}