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

export type RatePlanType = 'BAR' | 'non_refundable' | 'advance_purchase' | 'corporate' | 'government' | 'military' | 'long_stay' | 'package';
export type RatePlanStatus = 'active' | 'inactive' | 'scheduled' | 'expired';

export interface RatePlanRule {
  id?: number;
  rule_type: string;
  rule_key: string;
  rule_value?: unknown;
  price_modifier?: number;
  discount_percentage?: number;
  fixed_adjustment?: number;
  priority: number;
}

export interface RatePlan {
  id: number;
  name: string;
  code: string;
  description?: string;
  plan_type: RatePlanType;
  status: RatePlanStatus;
  base_price_modifier: number;
  discount_percentage?: number;
  fixed_discount?: number;
  room_type_id?: number;
  room_type_name?: string;
  min_nights?: number;
  max_nights?: number;
  advance_days_required?: number;
  valid_from?: string;
  valid_to?: string;
  is_refundable: boolean;
  requires_deposit: boolean;
  deposit_percentage?: number;
  cancellation_hours?: number;
  corporate_code?: string;
  requires_verification: boolean;
  verification_type?: string;
  long_stay_nights?: number;
  is_package: boolean;
  package_id?: number;
  priority: number;
  extra_data?: Record<string, unknown>;
  rules?: RatePlanRule[];
  created_at?: string;
  updated_at?: string;
}

export interface RatePlanListResponse {
  status: string;
  data: {
    rate_plans: RatePlan[];
    pagination?: {
      total: number;
      page: number;
      limit: number;
      totalPages: number;
    };
  };
}

export interface CreateRatePlanData {
  name: string;
  code: string;
  description?: string;
  plan_type: RatePlanType;
  status?: RatePlanStatus;
  base_price_modifier?: number;
  discount_percentage?: number;
  fixed_discount?: number;
  room_type_id?: number;
  min_nights?: number;
  max_nights?: number;
  advance_days_required?: number;
  valid_from?: string;
  valid_to?: string;
  is_refundable?: boolean;
  requires_deposit?: boolean;
  deposit_percentage?: number;
  cancellation_hours?: number;
  corporate_code?: string;
  requires_verification?: boolean;
  verification_type?: string;
  long_stay_nights?: number;
  is_package?: boolean;
  package_id?: number;
  priority?: number;
  extra_data?: Record<string, unknown>;
  rules?: RatePlanRule[];
}

export interface UpdateRatePlanData {
  name?: string;
  description?: string;
  status?: RatePlanStatus;
  base_price_modifier?: number;
  discount_percentage?: number;
  fixed_discount?: number;
  room_type_id?: number;
  min_nights?: number;
  max_nights?: number;
  advance_days_required?: number;
  valid_from?: string;
  valid_to?: string;
  is_refundable?: boolean;
  requires_deposit?: boolean;
  deposit_percentage?: number;
  cancellation_hours?: number;
  corporate_code?: string;
  requires_verification?: boolean;
  verification_type?: string;
  long_stay_nights?: number;
  package_id?: number;
  priority?: number;
  extra_data?: Record<string, unknown>;
}

export interface GetAvailableRatePlansParams {
  room_type_id: number;
  check_in: string;
  check_out: string;
  num_nights?: number;
}

class RatePlanService {
  async getRatePlans(params?: {
    search?: string;
    status?: string;
    plan_type?: string;
    room_type_id?: number;
    page?: number;
    limit?: number;
  }): Promise<RatePlanListResponse> {
    const response = await apiClient.get('/rate-plans', { params });
    return response.data;
  }

  async getRatePlan(id: number): Promise<{ status: string; data: { rate_plan: RatePlan } }> {
    const response = await apiClient.get(`/rate-plans/${id}`);
    return response.data;
  }

  async createRatePlan(data: CreateRatePlanData): Promise<{ status: string; message: string; data: { rate_plan_id: number } }> {
    const response = await apiClient.post('/rate-plans', data);
    return response.data;
  }

  async updateRatePlan(id: number, data: UpdateRatePlanData): Promise<{ status: string; message: string }> {
    const response = await apiClient.put(`/rate-plans/${id}`, data);
    return response.data;
  }

  async deleteRatePlan(id: number): Promise<{ status: string; message: string }> {
    const response = await apiClient.delete(`/rate-plans/${id}`);
    return response.data;
  }

  async getAvailableRatePlans(params: GetAvailableRatePlansParams): Promise<{ status: string; data: { rate_plans: RatePlan[] } }> {
    const response = await apiClient.get(`/rate-plans/available/${params.room_type_id}`, {
      params: {
        check_in: params.check_in,
        check_out: params.check_out,
        num_nights: params.num_nights,
      },
    });
    return response.data;
  }

  /**
   * Calculate the final price for a rate plan
   */
  calculatePrice(basePrice: number, ratePlan: RatePlan): number {
    let price = basePrice;

    // Apply base price modifier
    if (ratePlan.base_price_modifier) {
      price = price * ratePlan.base_price_modifier;
    }

    // Apply percentage discount
    if (ratePlan.discount_percentage) {
      price = price * (1 - ratePlan.discount_percentage / 100);
    }

    // Apply fixed discount
    if (ratePlan.fixed_discount) {
      price = price - ratePlan.fixed_discount;
    }

    return Math.max(0, price); // Ensure price is not negative
  }
}

const ratePlanService = new RatePlanService();
export default ratePlanService;

