import apiClient from '../../../shared/services/apiClient';

export interface Promotion {
  id: number;
  code: string;
  name: string;
  description?: string;
  discount_type: 'percentage' | 'fixed';
  discount_value: number;
  min_booking_amount?: number;
  max_discount_amount?: number;
  min_stay_days?: number;
  max_stay_days?: number;
  advance_booking_days?: number;
  max_advance_booking_days?: number;
  allowed_check_in_days?: number[];
  allowed_check_out_days?: number[];
  allowed_room_type_ids?: number[];
  excluded_room_type_ids?: number[];
  min_guests?: number;
  max_guests?: number;
  first_time_customer_only?: boolean;
  repeat_customer_only?: boolean;
  blackout_dates?: string[];
  start_date: string;
  end_date: string;
  usage_limit?: number;
  used_count?: number;
  status: 'active' | 'inactive' | 'expired';
  created_at?: string;
  updated_at?: string;
}

export interface PromotionListResponse {
  success: boolean;
  status?: string;
  data: {
    promotions: Promotion[];
    pagination?: {
      total: number;
      page: number;
      limit: number;
      totalPages: number;
    };
  };
  message?: string;
}

export interface CreatePromotionData {
  code: string;
  name: string;
  description?: string;
  discount_type: 'percentage' | 'fixed';
  discount_value: number;
  min_booking_amount?: number;
  max_discount_amount?: number;
  min_stay_days?: number;
  max_stay_days?: number;
  advance_booking_days?: number;
  max_advance_booking_days?: number;
  allowed_check_in_days?: number[];
  allowed_check_out_days?: number[];
  allowed_room_type_ids?: number[];
  excluded_room_type_ids?: number[];
  min_guests?: number;
  max_guests?: number;
  first_time_customer_only?: boolean;
  repeat_customer_only?: boolean;
  blackout_dates?: string[];
  start_date: string;
  end_date: string;
  usage_limit?: number;
  status?: 'active' | 'inactive' | 'expired';
}

export interface UpdatePromotionData {
  code?: string;
  name?: string;
  description?: string;
  discount_type?: 'percentage' | 'fixed';
  discount_value?: number;
  min_booking_amount?: number;
  max_discount_amount?: number;
  min_stay_days?: number;
  max_stay_days?: number;
  advance_booking_days?: number;
  max_advance_booking_days?: number;
  allowed_check_in_days?: number[];
  allowed_check_out_days?: number[];
  allowed_room_type_ids?: number[];
  excluded_room_type_ids?: number[];
  min_guests?: number;
  max_guests?: number;
  first_time_customer_only?: boolean;
  repeat_customer_only?: boolean;
  blackout_dates?: string[];
  start_date?: string;
  end_date?: string;
  usage_limit?: number;
  status?: 'active' | 'inactive' | 'expired';
}

export interface PromotionSearchParams {
  status?: string;
  search?: string;
  page?: number;
  limit?: number;
}

export const getPromotions = async (
  params: PromotionSearchParams = {}
): Promise<PromotionListResponse> => {
  const response = await apiClient.get('/promotions', { params });
  return response.data;
};

export const getPromotionById = async (
  id: number
): Promise<{ success: boolean; data: { promotion: Promotion } }> => {
  const response = await apiClient.get(`/promotions/${id}`);
  return response.data;
};

export const createPromotion = async (
  data: CreatePromotionData
): Promise<{ success: boolean; data: { promotion: Promotion }; message: string }> => {
  const response = await apiClient.post('/promotions', data);
  return response.data;
};

export const updatePromotion = async (
  id: number,
  data: UpdatePromotionData
): Promise<{ success: boolean; data: { promotion: Promotion }; message: string }> => {
  const response = await apiClient.put(`/promotions/${id}`, data);
  return response.data;
};

export const deletePromotion = async (
  id: number
): Promise<{ success: boolean; message: string }> => {
  const response = await apiClient.delete(`/promotions/${id}`);
  return response.data;
};

export const validatePromotion = async (
  code: string,
  bookingValue: number,
  checkInDate?: string,
  checkOutDate?: string,
  roomTypeId?: number,
  guestCount?: number,
  isFirstTimeCustomer?: boolean
): Promise<{ success: boolean; data: { promotion: Promotion; discount: number }; message: string }> => {
  const response = await apiClient.post('/promotions/validate', {
    code,
    booking_value: bookingValue,
    check_in_date: checkInDate,
    check_out_date: checkOutDate,
    room_type_id: roomTypeId,
    guest_count: guestCount,
    is_first_time_customer: isFirstTimeCustomer,
  });
  return response.data;
};

export default {
  getPromotions,
  getPromotionById,
  createPromotion,
  updatePromotion,
  deletePromotion,
  validatePromotion,
};
