import { api } from '@services/api/axios-instance'; import type { Coupon, CreateCouponDto, UpdateCouponDto, CouponValidation, CouponFilters, CouponsResponse, CouponStats, CouponRedemption, } from '../types'; const BASE_URL = '/api/v1/billing/coupons'; export const couponsApi = { getAll: async (filters?: CouponFilters): Promise => { const params = new URLSearchParams(); if (filters?.search) params.append('search', filters.search); if (filters?.discountType) params.append('discountType', filters.discountType); if (filters?.isActive !== undefined) params.append('isActive', String(filters.isActive)); if (filters?.page) params.append('page', String(filters.page)); if (filters?.limit) params.append('limit', String(filters.limit)); if (filters?.sortBy) params.append('sortBy', filters.sortBy); if (filters?.sortOrder) params.append('sortOrder', filters.sortOrder); const response = await api.get(`${BASE_URL}?${params.toString()}`); return response.data; }, getById: async (id: string): Promise => { const response = await api.get(`${BASE_URL}/${id}`); return response.data; }, getByCode: async (code: string): Promise => { const response = await api.get(`${BASE_URL}/code/${code}`); return response.data; }, create: async (data: CreateCouponDto): Promise => { const response = await api.post(BASE_URL, data); return response.data; }, update: async (id: string, data: UpdateCouponDto): Promise => { const response = await api.patch(`${BASE_URL}/${id}`, data); return response.data; }, delete: async (id: string): Promise => { await api.delete(`${BASE_URL}/${id}`); }, validate: async (code: string, planId?: string, amount?: number): Promise => { const response = await api.post(`${BASE_URL}/validate`, { code, planId, amount, }); return response.data; }, activate: async (id: string): Promise => { const response = await api.post(`${BASE_URL}/${id}/activate`); return response.data; }, deactivate: async (id: string): Promise => { const response = await api.post(`${BASE_URL}/${id}/deactivate`); return response.data; }, getStats: async (): Promise => { const response = await api.get(`${BASE_URL}/stats`); return response.data; }, getRedemptions: async ( couponId: string ): Promise<{ data: CouponRedemption[]; total: number }> => { const response = await api.get<{ data: CouponRedemption[]; total: number }>( `${BASE_URL}/${couponId}/redemptions` ); return response.data; }, };