﻿import React, { useState } from 'react';
import { Head, router } from '@inertiajs/react';
import { StaffLayout } from '@/Layouts/StaffLayout';
import { CurrencyPrice } from '@/Components/CurrencyPrice';
import { TrendingUp, Calendar, Download, Filter } from 'lucide-react';

interface Props {
    dateRange: { start: string; end: string };
    monthlyTrend: any[];
    roomTypePerformance: any[];
    taxSummary: {
        vat18: number;
        serviceCharge5: number;
        grossRevenue: number;
        netRevenue: number;
    };
}

export default function AdminReports({
    dateRange,
    monthlyTrend = [],
    roomTypePerformance = [],
    taxSummary = {
        vat18: 2673,
        serviceCharge5: 742.5,
        grossRevenue: 14850,
        netRevenue: 11434.5,
    },
}: Props) {
    const [start, setStart] = useState(dateRange?.start || '2026-01-01');
    const [end, setEnd] = useState(dateRange?.end || '2026-12-31');

    const handleFilter = (e: React.FormEvent) => {
        e.preventDefault();
        router.get('/ssadmin/reports', { start, end });
    };

    return (
        <StaffLayout title="Revenue & Financial Analytics BI">
            <Head title="BI Reports — Primon Hotel" />

            <div className="space-y-8">
                {/* Filter Bar */}
                <form onSubmit={handleFilter} className="bg-[#FBF9F5] p-5 rounded-2xl border border-[#C5A880]/30 flex flex-wrap items-center justify-between gap-4 shadow-sm text-xs">
                    <div className="flex flex-wrap items-center gap-4">
                        <span className="font-bold text-[#1A1A1A] uppercase tracking-wider text-[10px]">Filter Date Range:</span>
                        <input
                            type="date"
                            value={start}
                            onChange={(e) => setStart(e.target.value)}
                            className="bg-white border border-gray-300 rounded-lg px-3 py-1.5 text-xs"
                        />
                        <span>to</span>
                        <input
                            type="date"
                            value={end}
                            onChange={(e) => setEnd(e.target.value)}
                            className="bg-white border border-gray-300 rounded-lg px-3 py-1.5 text-xs"
                        />
                        <button
                            type="submit"
                            className="bg-[#1A1A1A] text-white px-4 py-1.5 rounded-lg font-bold uppercase tracking-wider text-[10px]"
                        >
                            Update BI
                        </button>
                    </div>
                </form>

                {/* Tax & Audited Ledger Summary */}
                <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
                    <div className="bg-[#FBF9F5] p-6 rounded-3xl border border-[#C5A880]/20 space-y-1">
                        <span className="text-[10px] uppercase text-gray-500 font-bold block">Gross Revenue (Audited)</span>
                        <span className="font-serif text-2xl font-bold text-[#1A1A1A] block"><CurrencyPrice amountUsd={taxSummary.grossRevenue} /></span>
                        <span className="text-[10px] text-gray-500">All room & outlet revenues</span>
                    </div>

                    <div className="bg-[#FBF9F5] p-6 rounded-3xl border border-[#C5A880]/20 space-y-1">
                        <span className="text-[10px] uppercase text-gray-500 font-bold block">URA VAT (18%)</span>
                        <span className="font-serif text-2xl font-bold text-gray-700 block"><CurrencyPrice amountUsd={taxSummary.vat18} /></span>
                        <span className="text-[10px] text-gray-500">Uganda Revenue Authority liability</span>
                    </div>

                    <div className="bg-[#FBF9F5] p-6 rounded-3xl border border-[#C5A880]/20 space-y-1">
                        <span className="text-[10px] uppercase text-gray-500 font-bold block">Service Charge Pool (5%)</span>
                        <span className="font-serif text-2xl font-bold text-gray-700 block"><CurrencyPrice amountUsd={taxSummary.serviceCharge5} /></span>
                        <span className="text-[10px] text-gray-500">Staff distribution pool</span>
                    </div>

                    <div className="bg-[#1A1A1A] text-[#FBF9F5] p-6 rounded-3xl border border-[#C5A880]/40 space-y-1">
                        <span className="text-[10px] uppercase text-[#C5A880] font-bold block">Net Operating Revenue</span>
                        <span className="font-serif text-2xl font-bold text-[#C5A880] block"><CurrencyPrice amountUsd={taxSummary.netRevenue} /></span>
                        <span className="text-[10px] text-gray-400">Net after statutory levies</span>
                    </div>
                </div>

                {/* Room Tier Revenue Matrix */}
                <div className="bg-[#FBF9F5] rounded-3xl border border-[#C5A880]/20 shadow-sm overflow-hidden p-6 space-y-4">
                    <h3 className="font-serif text-xl font-medium text-[#1A1A1A]">Room Category Profitability & Occupancy</h3>
                    <div className="overflow-x-auto">
                        <table className="w-full text-left text-xs">
                            <thead className="bg-[#F3EFEA] text-[10px] uppercase text-gray-600 border-b border-gray-200">
                                <tr>
                                    <th className="p-3">Room Tier</th>
                                    <th className="p-3">Total Physical Units</th>
                                    <th className="p-3">Occupancy Rate</th>
                                    <th className="p-3">ADR Realized</th>
                                    <th className="p-3 text-right">Gross Total Revenue</th>
                                </tr>
                            </thead>
                            <tbody className="divide-y divide-gray-200">
                                {roomTypePerformance.map((rt, i) => (
                                    <tr key={i} className="hover:bg-gray-50">
                                        <td className="p-3 font-bold text-[#1A1A1A]">{rt.name}</td>
                                        <td className="p-3">{rt.unitsCount || 6} Suites</td>
                                        <td className="p-3 font-semibold">{rt.occupancyRate || '82%'}</td>
                                        <td className="p-3 font-semibold">${rt.adr || rt.base_price_usd}</td>
                                        <td className="p-3 font-bold text-right text-[#C5A880]">
                                            <CurrencyPrice amountUsd={rt.revenue || (rt.base_price_usd * 20)} />
                                        </td>
                                    </tr>
                                ))}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </StaffLayout>
    );
}
