import { apiFetch, type PaginatedResponse } from './api'; export interface VehicleNested { id: string; make: string; model: string; fin: string; vehicle_type: string; availability: string; price: number; } export interface ContactNested { id: string; company_name: string; address_city?: string; address_country: string; vat_id?: string; email?: string; phone?: string; } export interface SaleResponse { id: string; vehicle_id: string; buyer_contact_id: string; seller_contact_id?: string | null; sale_price: number; sale_date: string; status: string; is_gwg: boolean; contract_pdf_path?: string | null; created_at?: string; updated_at?: string; vehicle?: VehicleNested; buyer?: ContactNested; seller?: ContactNested; } export interface SaleCreateData { vehicle_id: string; buyer_contact_id: string; seller_contact_id?: string; sale_price: number; sale_date?: string; status?: string; is_gwg?: boolean; } export interface SaleUpdateData { sale_price?: number; sale_date?: string; status?: string; is_gwg?: boolean; seller_contact_id?: string; } export interface SaleListParams { page?: number; page_size?: number; status?: string; date_from?: string; date_to?: string; } export interface ContractResponse { sale_id: string; contract_pdf_path: string; message: string; } export interface UstIdVerifyResponse { verified: boolean; message: string; vat_id?: string | null; } export async function listSales(params: SaleListParams = {}): Promise> { const query = new URLSearchParams(); if (params.page) query.set('page', String(params.page)); if (params.page_size) query.set('page_size', String(params.page_size)); if (params.status) query.set('status', params.status); if (params.date_from) query.set('date_from', params.date_from); if (params.date_to) query.set('date_to', params.date_to); return apiFetch>(`/sales/?${query.toString()}`); } export async function getSale(id: string): Promise { return apiFetch(`/sales/${id}`); } export async function createSale(data: SaleCreateData): Promise { return apiFetch('/sales/', { method: 'POST', body: JSON.stringify(data), }); } export async function updateSale(id: string, data: SaleUpdateData): Promise { return apiFetch(`/sales/${id}`, { method: 'PUT', body: JSON.stringify(data), }); } export async function deleteSale(id: string): Promise { return apiFetch(`/sales/${id}`, { method: 'DELETE' }); } export async function regenerateContract(id: string): Promise { return apiFetch(`/sales/${id}/contract`, { method: 'POST' }); } export async function verifyUstId(id: string): Promise { return apiFetch(`/sales/${id}/verify-ust-id`, { method: 'POST' }); } export function getContractDownloadUrl(id: string): string { const baseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api/v1'; return `${baseUrl}/sales/${id}/contract`; }