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

// ==================== REVENUE ANALYTICS ====================

export interface RevPARData {
  revpar: number;
  total_revenue: number;
  available_room_nights: number;
  period_days: number;
  total_rooms: number;
}

export interface ADRData {
  adr: number;
  period: {
    start: string | null;
    end: string | null;
  };
}

export interface OccupancyRateData {
  occupancy_rate: number;
  occupied_room_nights: number;
  available_room_nights: number;
  period_days: number;
}

export interface RevenueForecastData {
  forecast: Array<{
    date: string;
    forecasted_revenue: number;
    confidence: string;
  }>;
  average_daily_revenue: number;
  forecast_period: number;
  based_on_days: number;
}

export interface MarketPenetrationData {
  total_bookings: number;
  penetration_by_room_type: Array<{
    room_type: string;
    bookings: number;
    revenue: number;
    market_share: number;
  }>;
  period: {
    start: string;
    end: string;
  };
}

// ==================== OPERATIONAL ANALYTICS ====================

export interface StaffPerformanceData {
  staff_performance: Array<{
    staff_id: number;
    staff_name: string;
    email: string;
    check_ins_handled: number;
    performance_score: number;
  }>;
  period: {
    start: string;
    end: string;
  };
}

export interface ServiceUsageData {
  services: Array<{
    service_id: number;
    service_name: string;
    category: string;
    usage_count: number;
    total_revenue: number;
    average_price: number;
  }>;
  total_services: number;
  total_usage: number;
  total_revenue: number;
}

export interface OperationalEfficiencyData {
  conversion_rate: number;
  average_booking_value: number;
  cancellation_rate: number;
  total_bookings: number;
  confirmed_bookings: number;
  cancelled_bookings: number;
}

// ==================== GUEST ANALYTICS ====================

export interface GuestLTVData {
  guests: Array<{
    user_id: number;
    name: string;
    email: string;
    total_bookings: number;
    lifetime_value: number;
    average_booking_value: number;
  }>;
  average_ltv: number;
  total_guests_analyzed: number;
}

export interface CustomerAcquisitionCostData {
  new_customers: number;
  period: {
    start: string;
    end: string;
  };
  note: string;
}

export interface RepeatGuestRateData {
  repeat_guest_rate: number;
  total_guests: number;
  repeat_guests: number;
  one_time_guests: number;
}

export interface GuestSatisfactionTrendsData {
  trends: Array<{
    date: string;
    average_rating: number;
    review_count: number;
  }>;
  overall_average_rating: number;
  total_reviews: number;
}

// ==================== FINANCIAL ANALYTICS ====================

export interface ProfitLossData {
  total_revenue: number;
  refunds: number;
  net_revenue: number;
  gross_profit: number;
  period: {
    start: string;
    end: string;
  };
}

export interface PaymentMethodAnalyticsData {
  payment_methods: Array<{
    payment_method: string;
    transaction_count: number;
    total_amount: number;
    average_amount: number;
    percentage: number;
  }>;
  total_transactions: number;
  total_amount: number;
}

export interface RefundAnalysisData {
  refunds: Array<{
    date: string;
    refund_count: number;
    refund_amount: number;
  }>;
  total_refund_amount: number;
  total_refund_count: number;
  average_refund_amount: number;
}

// ==================== COMPREHENSIVE ANALYTICS ====================

export interface ComprehensiveAnalyticsData {
  period: {
    start: string | null;
    end: string | null;
  };
  revenue?: {
    revpar: RevPARData;
    adr: ADRData;
    occupancy_rate: OccupancyRateData;
    revenue_forecast: RevenueForecastData;
    market_penetration: MarketPenetrationData;
  };
  operational?: {
    staff_performance: StaffPerformanceData;
    service_usage: ServiceUsageData;
    operational_efficiency: OperationalEfficiencyData;
  };
  guest?: {
    lifetime_value: GuestLTVData;
    customer_acquisition_cost: CustomerAcquisitionCostData;
    repeat_guest_rate: RepeatGuestRateData;
    satisfaction_trends: GuestSatisfactionTrendsData;
  };
  financial?: {
    profit_loss: ProfitLossData;
    payment_methods: PaymentMethodAnalyticsData;
    refund_analysis: RefundAnalysisData;
  };
}

export interface AnalyticsParams {
  from?: string;
  to?: string;
  days?: number;
  include_revenue?: boolean;
  include_operational?: boolean;
  include_guest?: boolean;
  include_financial?: boolean;
}

// ==================== API FUNCTIONS ====================

// Revenue Analytics
export const getRevPAR = async (params?: AnalyticsParams): Promise<{ status: string; data: RevPARData }> => {
  const response = await apiClient.get('/analytics/revenue/revpar', { params });
  return response.data;
};

export const getADR = async (params?: AnalyticsParams): Promise<{ status: string; data: ADRData }> => {
  const response = await apiClient.get('/analytics/revenue/adr', { params });
  return response.data;
};

export const getOccupancyRate = async (params?: AnalyticsParams): Promise<{ status: string; data: OccupancyRateData }> => {
  const response = await apiClient.get('/analytics/revenue/occupancy', { params });
  return response.data;
};

export const getRevenueForecast = async (days: number = 30): Promise<{ status: string; data: RevenueForecastData }> => {
  const response = await apiClient.get('/analytics/revenue/forecast', { params: { days } });
  return response.data;
};

export const getMarketPenetration = async (params?: AnalyticsParams): Promise<{ status: string; data: MarketPenetrationData }> => {
  const response = await apiClient.get('/analytics/revenue/market-penetration', { params });
  return response.data;
};

// Operational Analytics
export const getStaffPerformance = async (params?: AnalyticsParams): Promise<{ status: string; data: StaffPerformanceData }> => {
  const response = await apiClient.get('/analytics/operational/staff-performance', { params });
  return response.data;
};

export const getServiceUsageAnalytics = async (params?: AnalyticsParams): Promise<{ status: string; data: ServiceUsageData }> => {
  const response = await apiClient.get('/analytics/operational/service-usage', { params });
  return response.data;
};

export const getOperationalEfficiency = async (params?: AnalyticsParams): Promise<{ status: string; data: OperationalEfficiencyData }> => {
  const response = await apiClient.get('/analytics/operational/efficiency', { params });
  return response.data;
};

// Guest Analytics
export const getGuestLifetimeValue = async (params?: AnalyticsParams): Promise<{ status: string; data: GuestLTVData }> => {
  const response = await apiClient.get('/analytics/guest/lifetime-value', { params });
  return response.data;
};

export const getCustomerAcquisitionCost = async (params?: AnalyticsParams): Promise<{ status: string; data: CustomerAcquisitionCostData }> => {
  const response = await apiClient.get('/analytics/guest/acquisition-cost', { params });
  return response.data;
};

export const getRepeatGuestRate = async (params?: AnalyticsParams): Promise<{ status: string; data: RepeatGuestRateData }> => {
  const response = await apiClient.get('/analytics/guest/repeat-rate', { params });
  return response.data;
};

export const getGuestSatisfactionTrends = async (params?: AnalyticsParams): Promise<{ status: string; data: GuestSatisfactionTrendsData }> => {
  const response = await apiClient.get('/analytics/guest/satisfaction-trends', { params });
  return response.data;
};

// Financial Analytics
export const getProfitLoss = async (params?: AnalyticsParams): Promise<{ status: string; data: ProfitLossData }> => {
  const response = await apiClient.get('/analytics/financial/profit-loss', { params });
  return response.data;
};

export const getPaymentMethodAnalytics = async (params?: AnalyticsParams): Promise<{ status: string; data: PaymentMethodAnalyticsData }> => {
  const response = await apiClient.get('/analytics/financial/payment-methods', { params });
  return response.data;
};

export const getRefundAnalysis = async (params?: AnalyticsParams): Promise<{ status: string; data: RefundAnalysisData }> => {
  const response = await apiClient.get('/analytics/financial/refunds', { params });
  return response.data;
};

// Comprehensive Analytics
export const getComprehensiveAnalytics = async (params?: AnalyticsParams): Promise<{ status: string; data: ComprehensiveAnalyticsData }> => {
  const response = await apiClient.get('/analytics/comprehensive', { params });
  return response.data;
};

const analyticsService = {
  // Revenue Analytics
  getRevPAR,
  getADR,
  getOccupancyRate,
  getRevenueForecast,
  getMarketPenetration,
  
  // Operational Analytics
  getStaffPerformance,
  getServiceUsageAnalytics,
  getOperationalEfficiency,
  
  // Guest Analytics
  getGuestLifetimeValue,
  getCustomerAcquisitionCost,
  getRepeatGuestRate,
  getGuestSatisfactionTrends,
  
  // Financial Analytics
  getProfitLoss,
  getPaymentMethodAnalytics,
  getRefundAnalysis,
  
  // Comprehensive
  getComprehensiveAnalytics,
};

export default analyticsService;

