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

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

function DiscountBadge({ product }: { product: Product }) {
    if (!product.is_discount_active) return null;
    const pct = product.discount_percent ?? 0;
    if (pct <= 0) return null;
    return (
        <span className="inline-flex items-center gap-1 rounded bg-red-100 px-2 py-0.5 text-xs font-bold text-red-600">
            -{pct}% OFF
        </span>
    );
}

export default function AdminProductShow({ product }: { product: Product }) {
    return (
        <AdminLayout title={product.name}>
            <Head title={product.name} />

            <div className="mb-4 flex items-center justify-between">
                <Button variant="ghost" size="sm" onClick={() => router.visit('/admin/products')}>
                    <ChevronLeft size={16} /> Back
                </Button>
                <Link href={route('admin.products.edit', product.id)}>
                    <Button size="sm" className="bg-indigo-600 hover:bg-indigo-700">
                        <Edit size={14} /> Edit
                    </Button>
                </Link>
            </div>

            <div className="grid gap-4 lg:grid-cols-3">
                <div className="lg:col-span-2 space-y-4">
                    <Card>
                        <CardHeader><CardTitle className="text-base">Images</CardTitle></CardHeader>
                        <CardContent>
                            {product.images && product.images.length > 0 ? (
                                <div className="flex flex-wrap gap-2">
                                    {product.images.map((img) => (
                                        <img key={img.id} src={`/storage/${img.image}`} className="h-28 w-28 rounded-lg border object-cover" alt="" />
                                    ))}
                                </div>
                            ) : (
                                <div className="h-28 w-28 rounded-lg bg-slate-100 flex items-center justify-center text-slate-400 text-xs">No image</div>
                            )}
                        </CardContent>
                    </Card>

                    <Card>
                        <CardHeader><CardTitle className="text-base">Description</CardTitle></CardHeader>
                        <CardContent>
                            {product.short_description && <p className="mb-2 text-sm font-medium text-slate-700">{product.short_description}</p>}
                            {product.description ? (
                                <div className="prose prose-sm max-w-none text-slate-600" dangerouslySetInnerHTML={{ __html: product.description }} />
                            ) : (
                                <p className="text-sm text-slate-400">No description</p>
                            )}
                        </CardContent>
                    </Card>

                    {product.variants && product.variants.length > 0 && (
                        <Card>
                            <CardHeader><CardTitle className="text-base">Variants</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">Name</th>
                                            <th className="px-4 py-2 text-left">SKU</th>
                                            <th className="px-4 py-2 text-right">Price</th>
                                            <th className="px-4 py-2 text-right">Stock</th>
                                        </tr>
                                    </thead>
                                    <tbody className="divide-y">
                                        {product.variants.map((v) => (
                                            <tr key={v.id}>
                                                <td className="px-4 py-2">{v.variant_name}</td>
                                                <td className="px-4 py-2 font-mono text-xs text-slate-500">{v.sku}</td>
                                                <td className="px-4 py-2 text-right">৳{Number(v.price).toLocaleString()}</td>
                                                <td className="px-4 py-2 text-right">{v.stock_qty}</td>
                                            </tr>
                                        ))}
                                    </tbody>
                                </table>
                            </CardContent>
                        </Card>
                    )}
                </div>

                <div className="space-y-4">
                    <Card>
                        <CardHeader><CardTitle className="text-base">Details</CardTitle></CardHeader>
                        <CardContent className="space-y-2 text-sm">
                            {[
                                ['SKU', product.sku],
                                ['Stock', product.stock_qty],
                                ['Category', product.category?.name ?? '—'],
                                ['Brand', product.brand?.name ?? '—'],
                                ['Status', product.status ? '✅ Active' : '❌ Inactive'],
                            ].map(([label, value]) => (
                                <div key={String(label)} className="flex justify-between gap-2 border-b pb-1 last:border-0">
                                    <span className="text-slate-500">{label}</span>
                                    <span className="font-medium text-right">{value}</span>
                                </div>
                            ))}
                            <div className="flex justify-between gap-2 border-b pb-1">
                                <span className="text-slate-500">Selling Price</span>
                                <div className="flex items-center gap-2">
                                    <span className="font-semibold">৳{Number(product.price).toLocaleString()}</span>
                                    <DiscountBadge product={product} />
                                </div>
                            </div>
                            {product.compare_price && Number(product.compare_price) > 0 && (
                                <div className="flex justify-between gap-2 border-b pb-1">
                                    <span className="text-slate-500">Original Price</span>
                                    <span className="font-medium text-slate-400 line-through">
                                        ৳{Number(product.compare_price).toLocaleString()}
                                    </span>
                                </div>
                            )}
                            <div className="flex justify-between gap-2 border-b pb-1">
                                <span className="text-slate-500">Cost Price</span>
                                <span className="font-medium">৳{Number(product.cost_price).toLocaleString()}</span>
                            </div>
                            {product.discount_type && product.discount_type !== 'none' && (
                                <>
                                    <div className="flex justify-between gap-2 border-b pb-1">
                                        <span className="text-slate-500">Discount Type</span>
                                        <span className="font-medium capitalize">{product.discount_type === 'percent' ? `${product.discount_value}% Off` : `৳${product.discount_value} Off`}</span>
                                    </div>
                                    {product.discount_label && (
                                        <div className="flex justify-between gap-2 border-b pb-1">
                                            <span className="text-slate-500">Label</span>
                                            <span className="font-medium">{product.discount_label}</span>
                                        </div>
                                    )}
                                    {(product.discount_starts_at || product.discount_ends_at) && (
                                        <div className="flex justify-between gap-2 border-b pb-1">
                                            <span className="text-slate-500">Schedule</span>
                                            <span className="text-right font-medium text-xs">
                                                {product.discount_starts_at ? new Date(product.discount_starts_at).toLocaleDateString() : '—'}
                                                {' → '}
                                                {product.discount_ends_at ? new Date(product.discount_ends_at).toLocaleDateString() : '∞'}
                                            </span>
                                        </div>
                                    )}
                                    <div className="flex justify-between gap-2 pb-1">
                                        <span className="text-slate-500">Discount Status</span>
                                        {product.is_discount_active ? (
                                            <span className="rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-700">Active</span>
                                        ) : (
                                            <span className="rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-500">Inactive</span>
                                        )}
                                    </div>
                                </>
                            )}
                        </CardContent>
                    </Card>

                    <Card>
                        <CardHeader><CardTitle className="text-base">Labels</CardTitle></CardHeader>
                        <CardContent className="flex flex-wrap gap-2 text-xs">
                            {product.is_featured && <span className="rounded-full bg-blue-100 px-2 py-0.5 text-blue-700">Featured</span>}
                            {product.is_best_seller && <span className="rounded-full bg-green-100 px-2 py-0.5 text-green-700">Best Seller</span>}
                            {product.is_trending && <span className="rounded-full bg-orange-100 px-2 py-0.5 text-orange-700">Trending</span>}
                            {product.is_new_arrival && <span className="rounded-full bg-purple-100 px-2 py-0.5 text-purple-700">New Arrival</span>}
                        </CardContent>
                    </Card>
                </div>
            </div>
        </AdminLayout>
    );
}
