import { Head, router } from '@inertiajs/react';
import { useState } from 'react';

import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import AdminLayout from '@/layouts/admin-layout';

interface DaySales { date: string; total_orders: number; revenue: number; discount: number; }
interface Summary { total_orders: number; revenue: number; discount: number; shipping: number; }

export default function AdminSalesReport({ dailySales, summary, filters }: { dailySales: DaySales[]; summary: Summary; filters: { from: string; to: string } }) {
    const [from, setFrom] = useState(filters.from);
    const [to, setTo] = useState(filters.to);

    const apply = () => router.get('/admin/reports/sales', { from, to }, { preserveState: true });

    return (
        <AdminLayout title="Sales Report">
            <Head title="Sales Report" />

            <div className="mb-4 flex flex-wrap items-end gap-3">
                <div className="space-y-1">
                    <Label>From</Label>
                    <Input type="date" value={from} onChange={(e) => setFrom(e.target.value)} className="w-44" />
                </div>
                <div className="space-y-1">
                    <Label>To</Label>
                    <Input type="date" value={to} onChange={(e) => setTo(e.target.value)} className="w-44" />
                </div>
                <Button onClick={apply} className="bg-indigo-600 hover:bg-indigo-700">Apply</Button>
            </div>

            {/* Summary */}
            <div className="mb-4 grid gap-3 sm:grid-cols-4">
                {[
                    ['Total Orders', summary?.total_orders ?? 0],
                    ['Revenue', `৳${Number(summary?.revenue ?? 0).toLocaleString()}`],
                    ['Discount Given', `৳${Number(summary?.discount ?? 0).toLocaleString()}`],
                    ['Shipping Collected', `৳${Number(summary?.shipping ?? 0).toLocaleString()}`],
                ].map(([label, val]) => (
                    <Card key={String(label)}>
                        <CardContent className="p-4">
                            <p className="text-xs text-slate-500">{label}</p>
                            <p className="mt-1 text-xl font-bold">{val}</p>
                        </CardContent>
                    </Card>
                ))}
            </div>

            <Card>
                <CardHeader><CardTitle className="text-base">Daily Breakdown</CardTitle></CardHeader>
                <CardContent className="p-0">
                    <table className="w-full text-sm">
                        <thead className="border-b bg-slate-50 text-xs uppercase text-slate-500">
                            <tr>
                                <th className="px-4 py-2 text-left">Date</th>
                                <th className="px-4 py-2 text-right">Orders</th>
                                <th className="px-4 py-2 text-right">Revenue</th>
                                <th className="px-4 py-2 text-right">Discount</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y">
                            {dailySales.map((row) => (
                                <tr key={row.date} className="hover:bg-slate-50">
                                    <td className="px-4 py-2">{row.date}</td>
                                    <td className="px-4 py-2 text-right">{row.total_orders}</td>
                                    <td className="px-4 py-2 text-right font-medium">৳{Number(row.revenue).toLocaleString()}</td>
                                    <td className="px-4 py-2 text-right text-slate-500">৳{Number(row.discount).toLocaleString()}</td>
                                </tr>
                            ))}
                            {dailySales.length === 0 && <tr><td colSpan={4} className="px-4 py-8 text-center text-slate-400">No data for this period</td></tr>}
                        </tbody>
                    </table>
                </CardContent>
            </Card>
        </AdminLayout>
    );
}
