import type { PaginatedResponse } from '../types/client';
import type {
  Sale,
  CreateSaleDto,
  UpdateSaleDto,
  FindSalesParams,
  DebtsSummary,
} from '../types/sale';
import { api } from './api';

export const salesService = {
  async getSales(params?: FindSalesParams): Promise<PaginatedResponse<Sale>> {
    return api.get<PaginatedResponse<Sale>>('/sales', {
      params: {
        cursor: params?.cursor,
        limit: params?.limit,
        trainerId: params?.trainerId,
        clientId: params?.clientId,
        status: params?.status,
        dateFrom: params?.dateFrom,
        dateTo: params?.dateTo,
      },
    });
  },

  async getDebts(params?: FindSalesParams): Promise<PaginatedResponse<Sale>> {
    return api.get<PaginatedResponse<Sale>>('/sales/debts', {
      params: {
        cursor: params?.cursor,
        limit: params?.limit,
        trainerId: params?.trainerId,
        clientId: params?.clientId,
        dateFrom: params?.dateFrom,
        dateTo: params?.dateTo,
      },
    });
  },

  async getDebtsSummary(): Promise<DebtsSummary> {
    return api.get<DebtsSummary>('/sales/debts/summary');
  },

  async getSale(id: string): Promise<Sale> {
    return api.get<Sale>(`/sales/${id}`);
  },

  async createSale(dto: CreateSaleDto): Promise<Sale> {
    return api.post<Sale>('/sales', dto);
  },

  async updateSale(id: string, dto: UpdateSaleDto): Promise<Sale> {
    return api.patch<Sale>(`/sales/${id}`, dto);
  },

  async markAsPaid(id: string): Promise<Sale> {
    return api.post<Sale>(`/sales/${id}/pay`);
  },

  async cancelSale(id: string): Promise<Sale> {
    return api.post<Sale>(`/sales/${id}/cancel`);
  },

  async refundSale(id: string): Promise<Sale> {
    return api.post<Sale>(`/sales/${id}/refund`);
  },
};
