/**
* YATRA TRAVEL BOOKING REPORTS
* Essential reports for travel booking businesses
* Based on deep understanding of Yatra business model
*/
import React, { useState, useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import { __ } from "../lib/i18n";
import { formatDateForInput } from "../lib/dateFormat";
import { apiClient, apiService } from "../lib/api-client";
import { unwrapApiPayload } from "../lib/unwrap-api-payload";
import { useToast } from "../components/ui/toast";
import {
Card,
CardContent,
CardHeader,
CardTitle,
CardDescription,
} from "../components/ui/card";
import { Button } from "../components/ui/button";
import { Select } from "../components/ui/select";
import BookingsOverviewChart from "../components/charts/BookingsOverviewChart";
import BookingStatusChart from "../components/charts/BookingStatusChart";
import { formatYatraMoney } from "../lib/currency-display";
import {
bucketSeries,
bucketStatusSeries,
buildCsv,
csvFilename,
downloadCsv,
type SeriesPoint,
type StatusPoint,
type TrendView,
} from "../lib/report-series";
import { isModuleActive, isProPluginActive } from "../lib/plugin-utils";
import { canCap } from "../hooks/useCapabilities";
// Skeleton Loading Components
const SkeletonCard = () => (
{__("Total Bookings", "yatra")}
{breakdownData.reduce((sum, row) => sum + row.bookings, 0)}
{__("Total Revenue", "yatra")}
{formatCurrencyAmount( breakdownData.reduce((sum, row) => sum + row.revenue, 0), )}
{__("Total Departures", "yatra")}
{breakdownData.reduce((sum, row) => sum + row.departures, 0)}
{__("Avg Occupancy", "yatra")}
{( breakdownData.reduce((sum, row) => sum + row.occupancyRate, 0) / breakdownData.length ).toFixed(1)} %
{!fbModuleActive ? __( "Enable the Facebook Pixel module under Modules and add your Pixel ID in Settings to start tracking conversion events.", "yatra", ) : __( "Configure your Facebook Pixel in Settings to start tracking conversion events.", "yatra", )}
{!fbModuleActive ? __("Open Modules", "yatra") : __("Configure Facebook Pixel", "yatra")}{__("Pixel Connection", "yatra")}
{facebookPixelData.connectionStatus?.pixelConnected ? __("Connected", "yatra") : __("Not Connected", "yatra")}
{__("API Token", "yatra")}
{facebookPixelData.connectionStatus?.tokenConnected ? __("Valid", "yatra") : __("Invalid", "yatra")}
{__("Pixel ID", "yatra")}
{facebookPixelData.pixel_id || __("Not Set", "yatra")}
{__( "Events will appear here once users start interacting with your site.", "yatra", )}
{!gaModuleActive ? __( "Enable the Google Analytics 4 module under Modules and add your Measurement ID in Settings to start tracking conversion events.", "yatra", ) : __( "Configure your Google Analytics 4 in Settings to start tracking conversion events.", "yatra", )}
{!gaModuleActive ? __("Open Modules", "yatra") : __("Configure Google Analytics 4", "yatra")}{__("Measurement ID", "yatra")}
{googleAnalyticsData.connectionStatus?.measurementConnected ? __("Connected", "yatra") : __("Not Connected", "yatra")}
{__("API Secret", "yatra")}
{googleAnalyticsData.connectionStatus?.apiSecretConnected ? __("Valid", "yatra") : __("Invalid", "yatra")}
{__("Measurement Protocol", "yatra")}
{googleAnalyticsData.use_measurement_protocol ? __("Enabled", "yatra") : __("Disabled", "yatra")}
{__( "Events will appear here once users start interacting with your site.", "yatra", )}
{__( "Essential analytics for your travel booking business.", "yatra", )}
{__("Total Bookings", "yatra")}
{travelKPIs.totalBookings}
{__("Total Revenue", "yatra")}
{formatCurrencyAmount(travelKPIs.totalRevenue)}
{__("Occupancy Rate", "yatra")}
{travelKPIs.occupancyRate.toFixed(1)}%
{__("Avg Booking Value", "yatra")}
{formatCurrencyAmount(travelKPIs.avgBookingValue)}
{__("Cancellation Rate", "yatra")}
{travelKPIs.cancellationRate.toFixed(1)}%
{__("Upcoming Departures", "yatra")}
{travelKPIs.upcomingDepartures}
{__("Confirmed Bookings", "yatra")}
{reportData?.booking_stats?.confirmed || 0}
{__("Pending Bookings", "yatra")}
{reportData?.booking_stats?.pending || 0}
{__("Cancelled Bookings", "yatra")}
{reportData?.booking_stats?.cancelled || 0}
{__("Completed Bookings", "yatra")}
{reportData?.booking_stats?.completed || 0}
Total Revenue
{formatCurrencyAmount( reportData?.revenue_stats?.total || 0, )}
Average Booking Value
{formatCurrencyAmount( reportData?.revenue_stats?.average || 0, )}
Revenue Lost (Cancellations)
{formatCurrencyAmount( reportData?.cancellations?.revenueLost || 0, )}
{trip.label}
{trip.value} bookings
{formatCurrencyAmount(trip.revenue || 0)}
Revenue
Upcoming Departures
{reportData?.operational_stats?.upcomingDepartures || 0}
Total Capacity
{reportData?.operational_stats?.totalCapacity || 0}
Booked Capacity
{reportData?.operational_stats?.bookedCapacity || 0}
Total Customers
{reportData?.customer_analytics?.totalCustomers || 0}
New Customers
{reportData?.customer_analytics?.newCustomers || 0}
Returning Customers
{reportData?.customer_analytics?.returningCustomers || 0}
Customer Lifetime Value
{formatCurrencyAmount( reportData?.customer_analytics ?.customerLifetimeValue || 0, )}
Occupancy Rate
{( reportData?.operational_stats?.occupancyRate || 0 ).toFixed(1)} %
Average Group Size
{( reportData?.operational_stats?.averageGroupSize || 0 ).toFixed(1)}
Cancellation Rate
{( reportData?.booking_stats?.cancellationRate || 0 ).toFixed(1)} %
{__( "Bookings and gross revenue split by payment gateway. Ranked by revenue.", "yatra", )}
{m.method}
{formatCurrencyAmount(m.revenue)}
{m.count} {__("bookings", "yatra")}
{__( "Booking count and revenue by primary destination. Useful for spotting geographic concentration.", "yatra", )}
{__( "Time between booking creation and travel date. Average:", "yatra", )}{" "} {Number(reportData.lead_time.averageDays || 0).toFixed( 1, )}{" "} {__("days", "yatra")} {" "} ({reportData.lead_time.sampleSize}{" "} {__("bookings sampled", "yatra")})
{__("Refunds issued", "yatra")}
{reportData?.refunds?.count ?? 0}
{__("Refund total", "yatra")}
{formatCurrencyAmount( Number(reportData?.refunds?.total) || 0, )}
{__("Refund rate", "yatra")}
{Number(reportData?.refunds?.refundRate || 0).toFixed( 1, )} %
{__("Avg refund", "yatra")}
{formatCurrencyAmount( Number(reportData?.refunds?.avgRefund) || 0, )}