import { Head, Link, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';

import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import AdminLayout from '@/layouts/admin-layout';
import { type Coupon, type PaginatedData } from '@/types';

export default function AdminCouponsIndex({ coupons }: { coupons: PaginatedData<Coupon> }) {
    const destroy = (id: number) => {
        if (confirm('Delete this coupon?')) router.delete(route('admin.coupons.destroy', id));
    };

    return (
        <AdminLayout title="Coupons">
            <Head title="Coupons" />
            <div className="mb-4 flex justify-end">
                <Link href={route('admin.coupons.create')}>
                    <Button className="bg-indigo-600 hover:bg-indigo-700"><Plus size={16} /> Add Coupon</Button>
                </Link>
            </div>
            <Card>
                <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-3 text-left">Code</th>
                                <th className="px-4 py-3 text-left">Type</th>
                                <th className="px-4 py-3 text-right">Value</th>
                                <th className="px-4 py-3 text-right">Min Order</th>
                                <th className="px-4 py-3 text-center">Used</th>
                                <th className="px-4 py-3 text-left">Expires</th>
                                <th className="px-4 py-3 text-center">Status</th>
                                <th className="px-4 py-3" />
                            </tr>
                        </thead>
                        <tbody className="divide-y">
                            {coupons.data.map((coupon) => (
                                <tr key={coupon.id} className="hover:bg-slate-50">
                                    <td className="px-4 py-3 font-mono font-bold tracking-wider">{coupon.code}</td>
                                    <td className="px-4 py-3 capitalize text-slate-500">{coupon.type}</td>
                                    <td className="px-4 py-3 text-right font-medium">
                                        {coupon.type === 'percent' ? `${coupon.value}%` : `৳${coupon.value}`}
                                    </td>
                                    <td className="px-4 py-3 text-right text-slate-500">৳{Number(coupon.min_order_amount).toLocaleString()}</td>
                                    <td className="px-4 py-3 text-center text-slate-500">
                                        {coupon.used_count}{coupon.usage_limit ? `/${coupon.usage_limit}` : ''}
                                    </td>
                                    <td className="px-4 py-3 text-slate-500 text-xs">{coupon.end_at ? new Date(coupon.end_at).toLocaleDateString('en-BD') : '—'}</td>
                                    <td className="px-4 py-3 text-center">
                                        <span className={`rounded-full px-2 py-0.5 text-xs font-medium ${coupon.status ? 'bg-green-100 text-green-700' : 'bg-slate-100 text-slate-500'}`}>
                                            {coupon.status ? 'Active' : 'Inactive'}
                                        </span>
                                    </td>
                                    <td className="px-4 py-3">
                                        <div className="flex gap-2">
                                            <Link href={route('admin.coupons.edit', coupon.id)} className="text-xs text-indigo-600 hover:underline">Edit</Link>
                                            <button onClick={() => destroy(coupon.id)} className="text-xs text-red-500 hover:underline">Delete</button>
                                        </div>
                                    </td>
                                </tr>
                            ))}
                            {coupons.data.length === 0 && <tr><td colSpan={8} className="px-4 py-10 text-center text-slate-400">No coupons yet</td></tr>}
                        </tbody>
                    </table>
                </CardContent>
            </Card>
        </AdminLayout>
    );
}
