import { apiClient } from '@/lib/api-client';

export interface PromoCode {
  id: string;
  code: string;
  title: string;
  discountType: 'PERCENTAGE' | 'FIXED';
  discountValue: number;
  maxDiscount?: number;
  minBookingAmount?: number;
  maxUsagePerUser?: number;
  totalUsageLimit?: number;
  usageCount: number;
  validFrom: string;
  validUntil: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

export const promosApi = {
  getAll: (filters?: { search?: string; active?: string; page?: number; pageSize?: number }) => {
    const params = new URLSearchParams();
    if (filters?.search) params.append('search', filters.search);
    if (filters?.active) params.append('active', filters.active);
    if (filters?.page) params.append('page', filters.page.toString());
    if (filters?.pageSize) params.append('pageSize', filters.pageSize.toString());

    return apiClient.get<{ items: PromoCode[]; total: number; page: number; pageSize: number }>(`/promos/all?${params.toString()}`);
  },

  getById: (id: string) => {
    return apiClient.get<PromoCode>(`/promos/${id}`);
  },

  create: (data: Omit<PromoCode, 'id' | 'createdAt' | 'updatedAt' | 'usageCount'>) => {
    return apiClient.post<PromoCode>('/promos', data);
  },

  update: (id: string, data: Partial<PromoCode>) => {
    return apiClient.patch<PromoCode>(`/promos/${id}`, data);
  },

  delete: (id: string) => {
    return apiClient.delete<void>(`/promos/${id}`);
  },

  validate: (code: string, bookingAmount?: number) => {
    return apiClient.post<{ valid: boolean; message?: string; discount?: number }>('/promos/validate', {
      code,
      bookingAmount,
    });
  },
};
