import AdminLayout from '@/Layouts/AdminLayout';
import { Head, Link, router } from '@inertiajs/react';
import { FileText, Layout, Settings, Megaphone, Target, ExternalLink, Edit, Trash2 } from 'lucide-react';

interface Product {
    id: number;
    name: string;
    description: string | null;
    price: number;
    currency: string;
    type: string;
    slug: string;
}

const getIcon = (name: string) => {
    if (name.includes('Playbook'))  return FileText;
    if (name.includes('Framework')) return Layout;
    if (name.includes('Bundle'))    return Settings;
    if (name.includes('Planner'))   return Megaphone;
    return Target;
};

export default function Index({ products }: { products: Product[] }) {
    const deleteProduct = (id: number) => {
        if (confirm('Delete this product?')) {
            router.delete(route('admin.products.destroy', id));
        }
    };

    return (
        <AdminLayout>
            <Head title="Products" />

            {/* Header */}
            <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-6 mb-10">
                <div>
                    <h1 className="font-display text-[1.5rem] md:text-[1.75rem] font-normal tracking-[-0.02em] text-ink leading-tight">
                        Products
                    </h1>
                    <p className="mt-2 text-[13px] md:text-[14px] text-[#8C92AC]">
                        {products.length} product{products.length !== 1 ? 's' : ''} live in the store.
                    </p>
                </div>
                <Link
                    href={route('admin.products.create')}
                    className="h-10 flex items-center justify-center px-6 bg-ink text-white text-[12px] font-semibold rounded-lg hover:bg-primary transition-colors w-full sm:w-auto"
                >
                    New product
                </Link>
            </div>

            {/* Mobile View: Cards */}
            <div className="grid gap-3 md:hidden mb-8">
                {products.length > 0 ? products.map((p) => {
                    const Icon = getIcon(p.name);
                    return (
                        <div key={p.id} className="bg-white rounded-[1.5rem] border border-[#E8E6DF] p-5 shadow-sm">
                            <div className="flex items-start gap-4 mb-4">
                                <div className="h-11 w-11 flex items-center justify-center bg-[#F5F3EF] border border-[#E8E6DF] rounded-xl text-[#8C92AC] flex-shrink-0">
                                    <Icon className="h-5 w-5" strokeWidth={1.5} />
                                </div>
                                <div className="flex-1 min-w-0">
                                    <div className="flex items-start justify-between gap-2">
                                        <p className="text-[14px] font-bold text-ink leading-tight">{p.name}</p>
                                        <div className="text-right flex-shrink-0">
                                            <p className="text-[13px] font-bold text-ink">₦{p.price.toLocaleString()}</p>
                                            <p className="text-[10px] text-[#8C92AC] mt-0.5">${(p.price / 1500).toFixed(0)}</p>
                                        </div>
                                    </div>
                                    <p className="text-[11px] font-bold text-[#8C92AC] mt-1.5 uppercase tracking-tighter">{p.type}</p>
                                </div>
                            </div>
                            
                            <div className="flex items-center justify-between pt-4 border-t border-[#F0EDE8]">
                                <div className="flex items-center gap-2">
                                    <Link href={route('admin.products.edit', p.id)} className="flex items-center gap-2 px-4 py-2 bg-[#F5F3EF] text-[11px] font-bold text-ink rounded-lg border border-[#E8E6DF]">
                                        <Edit className="h-3.5 w-3.5" /> Edit
                                    </Link>
                                    <Link href={`/store/${p.slug}`} target="_blank" className="p-2 text-[#8C92AC] hover:text-ink">
                                        <ExternalLink className="h-4 w-4" />
                                    </Link>
                                </div>
                                <button onClick={() => deleteProduct(p.id)} className="p-2 text-[#8C92AC] hover:text-red-500">
                                    <Trash2 className="h-4 w-4" />
                                </button>
                            </div>
                        </div>
                    );
                }) : (
                    <div className="bg-white rounded-[1.5rem] border border-[#E8E6DF] p-12 text-center text-[13px] text-[#8C92AC]">
                        No products found.
                    </div>
                )}
            </div>

            {/* Desktop View: Table */}
            <div className="hidden md:block bg-white rounded-xl border border-[#E8E6DF] overflow-hidden">
                <table className="w-full text-left border-collapse">
                    <thead>
                        <tr className="border-b border-[#F0EDE8]">
                            {['Product', 'Category', 'Tags', 'NGN', 'USD', ''].map((h, i) => (
                                <th
                                    key={i}
                                    className={`px-6 py-3.5 text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC] ${i >= 3 && i < 5 ? 'text-right' : ''}`}
                                >
                                    {h}
                                </th>
                            ))}
                        </tr>
                    </thead>
                    <tbody>
                        {products.length > 0 ? products.map((p) => {
                            const Icon = getIcon(p.name);
                            return (
                                <tr
                                    key={p.id}
                                    className="border-b border-[#F0EDE8] last:border-0 hover:bg-[#FAFAF8] transition-colors group"
                                >
                                    <td className="px-6 py-4">
                                        <div className="flex items-center gap-3">
                                            <div className="h-8 w-8 flex items-center justify-center bg-[#F5F3EF] border border-[#E8E6DF] rounded-lg text-[#8C92AC] flex-shrink-0">
                                                <Icon className="h-4 w-4" strokeWidth={1.5} />
                                            </div>
                                            <div>
                                                <p className="text-[13px] font-semibold text-ink">{p.name}</p>
                                                <p className="text-[11px] text-[#8C92AC] mt-0.5 max-w-xs truncate">
                                                    {p.description || p.slug}
                                                </p>
                                            </div>
                                        </div>
                                    </td>
                                    <td className="px-6 py-4">
                                        <span className="px-2.5 py-1 rounded-full border border-[#E8E6DF] text-[10px] font-semibold tracking-wide text-[#8C92AC]">
                                            {p.type}
                                        </span>
                                    </td>
                                    <td className="px-6 py-4">
                                        <span className="px-2 py-0.5 bg-[#F5F3EF] rounded text-[9px] font-bold tracking-widest text-[#8C92AC]">
                                            {p.type === 'Resource' ? 'ASSET' : 'LMS'}
                                        </span>
                                    </td>
                                    <td className="px-6 py-4 text-right text-[13px] font-semibold text-ink">
                                        {p.currency === 'NGN' ? `₦${p.price.toLocaleString()}` : '—'}
                                    </td>
                                    <td className="px-6 py-4 text-right text-[13px] font-semibold text-ink">
                                        {p.currency === 'USD' ? `$${p.price.toLocaleString()}` : `$${(p.price / 1500).toFixed(0)}`}
                                    </td>
                                    <td className="px-6 py-4">
                                        <div className="flex items-center justify-end gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
                                            <Link href={`/store/${p.slug}`} target="_blank" className="p-1.5 text-[#8C92AC] hover:text-ink rounded transition-colors">
                                                <ExternalLink className="h-3.5 w-3.5" />
                                            </Link>
                                            <Link href={route('admin.products.edit', p.id)} className="p-1.5 text-[#8C92AC] hover:text-ink rounded transition-colors">
                                                <Edit className="h-3.5 w-3.5" />
                                            </Link>
                                            <button onClick={() => deleteProduct(p.id)} className="p-1.5 text-[#8C92AC] hover:text-red-500 rounded transition-colors">
                                                <Trash2 className="h-3.5 w-3.5" />
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            );
                        }) : (
                            <tr>
                                <td colSpan={6} className="px-6 py-16 text-center text-[13px] text-[#8C92AC]">
                                    No products yet. Create your first product.
                                </td>
                            </tr>
                        )}
                    </tbody>
                </table>
            </div>
        </AdminLayout>
    );
}
