import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, Link, router } from '@inertiajs/react';
import { Badge } from '@/Components/ui/badge';
import { LinkButton } from '@/Components/ui/button';
import { formatCurrency, formatDate } from '@/lib/utils';
import { Edit, ExternalLink } from 'lucide-react';

export default function SupplierShow({ supplier, orders, totalSpend }: any) {
    return (
        <AuthenticatedLayout header={supplier.name}>
            <Head title={supplier.name} />
            <div className="space-y-6">
                <div className="flex gap-3"><LinkButton href={`/suppliers/${supplier.id}/edit`}><Edit className="w-4 h-4" />Edit</LinkButton><LinkButton href="/suppliers" variant="outline">← Back</LinkButton></div>
                <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
                    <div className="bg-white rounded-lg border border-border shadow-sm p-5 space-y-3">
                        <div className="flex justify-between items-start"><h2 className="font-semibold">Details</h2><Badge status={supplier.status} /></div>
                        {[['Contact',supplier.contact_name],['Email',supplier.email],['Phone',supplier.phone],['Address',supplier.address],['Reg No',supplier.company_reg_no],['Tax ID',supplier.tax_id],['Payment Terms',supplier.payment_terms]].map(([l,v])=>v?<div key={l}><p className="text-xs text-muted-foreground">{l}</p><p className="text-sm">{v}</p></div>:null)}
                    </div>
                    <div className="lg:col-span-2 space-y-4">
                        <div className="bg-white rounded-lg border border-border shadow-sm p-5">
                            <div className="flex justify-between items-center mb-4"><h2 className="font-semibold">Purchase Orders ({supplier.purchase_orders_count})</h2><p className="text-sm font-semibold">{formatCurrency(totalSpend)} total</p></div>
                            <table className="w-full text-sm"><thead className="border-b"><tr><th className="text-left py-2 font-medium">PO #</th><th className="text-left py-2 font-medium">Date</th><th className="text-left py-2 font-medium">Status</th><th className="text-right py-2 font-medium">Amount</th></tr></thead>
                            <tbody className="divide-y">{orders.map((o:any)=><tr key={o.id} className="hover:bg-muted/20"><td className="py-2"><Link href={`/purchase-orders/${o.id}`} className="font-medium hover:underline">{o.po_number??'Draft'}</Link></td><td className="py-2 text-muted-foreground">{formatDate(o.created_at)}</td><td className="py-2"><Badge status={o.status} /></td><td className="py-2 text-right font-medium">{formatCurrency(o.total_amount)}</td></tr>)}</tbody></table>
                            {orders.length===0&&<p className="text-center py-8 text-muted-foreground">No purchase orders yet.</p>}
                        </div>
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
