import { Head, Link } from "@inertiajs/react";
import { ArrowLeft } from "lucide-react";

import AppLayout from "@/layouts/app-layout";
import { Button } from "@/components/ui/button";
import { StatusBadge } from "@/components/crm-data-table";
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from "@/components/ui/table";

type OrderItem = {
    id: number;
    product?: { name: string };
    qty: number;
    unit_price: number | string;
    discount_amount: number | string;
    subtotal: number | string;
};

type OrderDetail = {
    id: number;
    order_number: string;
    order_date: string;
    status: string;
    subtotal: number | string;
    discount_total: number | string;
    tax_total: number | string;
    grand_total: number | string;
    customer?: { name: string };
    sales?: { name: string };
    items?: OrderItem[];
};

type Props = { order: OrderDetail };

export default function Show({ order }: Props) {
    // Format tanggal ke lokal Indonesia
    const formattedDate = order.order_date
        ? new Date(order.order_date).toLocaleDateString("id-ID", {
              day: "2-digit",
              month: "short",
              year: "numeric",
          })
        : "-";

    return (
        <AppLayout
            breadcrumbs={[
                { title: "Orders", href: "/crm/orders" },
                { title: order.order_number, href: `/crm/orders/${order.id}` },
            ]}
        >
            <Head title={`Detail ${order.order_number}`} />

            <div className="crm-page space-y-5">
                {/* Header Page */}
                <div className="flex flex-col justify-between gap-4 md:flex-row md:items-center">
                    <div className="flex items-center gap-3">
                        <Link href="/crm/orders">
                            <Button
                                variant="outline"
                                size="icon"
                                className="h-9 w-9"
                            >
                                <ArrowLeft className="h-4 w-4 text-muted-foreground" />
                                <span className="sr-only">Kembali</span>
                            </Button>
                        </Link>
                        <div>
                            <div className="flex items-center gap-2.5">
                                <h1 className="text-2xl font-semibold tracking-tight">
                                    {order.order_number}
                                </h1>
                                <StatusBadge status={order.status} />
                            </div>
                            <p className="text-sm text-muted-foreground mt-0.5">
                                Tanggal Order: {formattedDate}
                            </p>
                        </div>
                    </div>
                </div>

                {/* Info Cards */}
                <div className="grid gap-4 md:grid-cols-3">
                    <div className="rounded-xl border bg-card p-5 shadow-sm">
                        <div className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
                            Customer
                        </div>
                        <div className="mt-1 text-base font-semibold text-foreground">
                            {order.customer?.name || "-"}
                        </div>
                    </div>

                    <div className="rounded-xl border bg-card p-5 shadow-sm">
                        <div className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
                            Sales
                        </div>
                        <div className="mt-1 text-base font-semibold text-foreground">
                            {order.sales?.name || "-"}
                        </div>
                    </div>

                    <div className="rounded-xl border bg-card p-5 shadow-sm">
                        <div className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
                            Total Grand
                        </div>
                        <div className="mt-1 text-xl font-bold text-blue-600">
                            Rp{" "}
                            {Number(order.grand_total).toLocaleString("id-ID")}
                        </div>
                    </div>
                </div>

                {/* Tabel Item Order */}
                <div className="overflow-hidden rounded-xl border bg-card shadow-sm">
                    <div className="px-5 py-4 border-b">
                        <h2 className="text-sm font-semibold text-foreground">
                            Rincian Produk
                        </h2>
                    </div>
                    <Table className="w-full text-sm">
                        <TableHeader className="bg-muted/50/50">
                            <TableRow>
                                <TableHead className="px-5 py-3 text-left font-medium text-muted-foreground">
                                    Produk
                                </TableHead>
                                <TableHead className="px-5 py-3 text-center font-medium text-muted-foreground">
                                    Qty
                                </TableHead>
                                <TableHead className="px-5 py-3 text-right font-medium text-muted-foreground">
                                    Harga (Rp)
                                </TableHead>
                                <TableHead className="px-5 py-3 text-right font-medium text-muted-foreground">
                                    Diskon (Rp)
                                </TableHead>
                                <TableHead className="px-5 py-3 text-right font-medium text-muted-foreground">
                                    Subtotal (Rp)
                                </TableHead>
                            </TableRow>
                        </TableHeader>
                        <TableBody>
                            {order.items && order.items.length > 0 ? (
                                order.items.map((item) => (
                                    <TableRow
                                        className="border-t hover:bg-muted/50/50 transition-colors"
                                        key={item.id}
                                    >
                                        <TableCell className="px-5 py-3.5 font-medium text-foreground">
                                            {item.product?.name || "-"}
                                        </TableCell>
                                        <TableCell className="px-5 py-3.5 text-center">
                                            {item.qty}
                                        </TableCell>
                                        <TableCell className="px-5 py-3.5 text-right">
                                            Rp{" "}
                                            {Number(
                                                item.unit_price,
                                            ).toLocaleString("id-ID")}
                                        </TableCell>
                                        <TableCell className="px-5 py-3.5 text-right text-muted-foreground">
                                            Rp{" "}
                                            {Number(
                                                item.discount_amount,
                                            ).toLocaleString("id-ID")}
                                        </TableCell>
                                        <TableCell className="px-5 py-3.5 text-right font-medium text-foreground">
                                            Rp{" "}
                                            {Number(
                                                item.subtotal,
                                            ).toLocaleString("id-ID")}
                                        </TableCell>
                                    </TableRow>
                                ))
                            ) : (
                                <TableRow>
                                    <TableCell
                                        colSpan={5}
                                        className="text-center py-6 text-muted-foreground"
                                    >
                                        Tidak ada item dalam order ini.
                                    </TableCell>
                                </TableRow>
                            )}
                        </TableBody>
                    </Table>
                </div>

                {/* Ringkasan Pembayaran */}
                <div className="flex justify-end">
                    <div className="w-full max-w-sm rounded-xl border bg-card p-5 shadow-sm space-y-3">
                        <div className="flex justify-between text-sm text-muted-foreground">
                            <span>Subtotal</span>
                            <span className="font-medium text-foreground">
                                Rp{" "}
                                {Number(order.subtotal).toLocaleString("id-ID")}
                            </span>
                        </div>
                        <div className="flex justify-between text-sm text-muted-foreground">
                            <span>Diskon</span>
                            <span className="font-medium text-foreground">
                                Rp{" "}
                                {Number(order.discount_total).toLocaleString(
                                    "id-ID",
                                )}
                            </span>
                        </div>
                        <div className="flex justify-between text-sm text-muted-foreground">
                            <span>Pajak</span>
                            <span className="font-medium text-foreground">
                                Rp{" "}
                                {Number(order.tax_total).toLocaleString(
                                    "id-ID",
                                )}
                            </span>
                        </div>
                        <div className="flex justify-between border-t border-border pt-3 text-base font-bold text-foreground">
                            <span>Total</span>
                            <span className="text-blue-600">
                                Rp{" "}
                                {Number(order.grand_total).toLocaleString(
                                    "id-ID",
                                )}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </AppLayout>
    );
}
