/** Service for coupon-related API calls */
import { apiClient } from './apiClient'
import type { Coupon } from '@/types'

export interface CouponFilters {
    category?: string
    is_active?: boolean
    active_only?: boolean
}

export const couponsService = {
    /** Get all coupons with optional filters */
    async getAllCoupons(filters?: CouponFilters): Promise<Coupon[]> {
        const response = await apiClient.get<Coupon[]>('/api/coupons', {
            params: filters,
        })
        return response.data
    },

    /** Get a single coupon by ID */
    async getCouponById(id: string): Promise<Coupon> {
        const response = await apiClient.get<Coupon>(`/api/coupons/${id}`)
        return response.data
    },

    /** Create a new coupon */
    async createCoupon(coupon: Omit<Coupon, 'id' | 'created_at' | 'updated_at'>): Promise<Coupon> {
        const response = await apiClient.post<Coupon>('/api/coupons', coupon)
        return response.data
    },

    /** Update an existing coupon */
    async updateCoupon(id: string, coupon: Partial<Coupon>): Promise<Coupon> {
        const response = await apiClient.patch<Coupon>(`/api/coupons/${id}`, coupon)
        return response.data
    },

    /** Delete a coupon */
    async deleteCoupon(id: string): Promise<void> {
        await apiClient.delete(`/api/coupons/${id}`)
    },

    /** Claim a coupon */
    async claimCoupon(id: string): Promise<any> {
        const response = await apiClient.post(`/api/coupons/${id}/claim`)
        return response.data
    },

    /** Get current user's claimed coupons */
    async getMyCoupons(): Promise<any[]> {
        const response = await apiClient.get('/api/coupons/my-coupons')
        return response.data
    },

    /** Validate a coupon by code (merchants/drivers only) */
    async validateCoupon(code: string): Promise<any> {
        const response = await apiClient.post(`/api/coupons/validate/${code}`)
        return response.data
    }
}

