import type { Metadata } from "next";
import { prisma } from "@/lib/prisma";
import { notFound } from "next/navigation";

export const metadata: Metadata = {
    title: "Order Receipt",
    robots: { index: false, follow: false },
};

export default async function OrderReceiptPage({ params }: { params: Promise<{ id: string }> }) {
    const { id } = await params;

    const order = await prisma.order.findUnique({
        where: { id },
        include: { items: { include: { product: true } } },
    });

    if (!order) notFound();

    return (
        <div className="container" style={{ padding: "4rem 1.5rem", maxWidth: "600px" }}>
            <div className="glass-panel animate-fade-in" style={{ padding: "3rem" }}>
                <h1 style={{ fontSize: "1.75rem", marginBottom: "0.25rem" }}>Receipt</h1>
                <p style={{ color: "var(--text-muted)", marginBottom: "2rem" }}>
                    Order #{order.id} &middot; {order.createdAt.toLocaleString("en-US")}
                </p>

                <div style={{ display: "flex", flexDirection: "column", gap: "1rem", marginBottom: "2rem" }}>
                    {order.items.map((item) => {
                        const customData: Record<string, string> | null = item.customData
                            ? JSON.parse(item.customData)
                            : null;
                        return (
                            <div key={item.id} style={{ display: "flex", justifyContent: "space-between", paddingBottom: "1rem", borderBottom: "1px solid var(--border)" }}>
                                <div>
                                    <div style={{ fontWeight: 600 }}>{item.quantity}x {item.product.name}</div>
                                    {customData && (
                                        <div style={{ fontSize: "0.85rem", color: "var(--text-muted)", marginTop: "0.25rem" }}>
                                            {Object.values(customData).filter(Boolean).join(", ")}
                                        </div>
                                    )}
                                </div>
                                <div style={{ fontWeight: 600 }}>${(item.price * item.quantity).toFixed(2)}</div>
                            </div>
                        );
                    })}
                </div>

                <div style={{ display: "flex", justifyContent: "space-between", fontSize: "1.25rem", fontWeight: 700, marginBottom: "2rem" }}>
                    <span>Total</span>
                    <span style={{ color: "var(--primary)" }}>${order.totalAmount.toFixed(2)}</span>
                </div>

                <p style={{ fontSize: "0.85rem", color: "var(--text-muted)" }}>
                    Billed to {order.customerName} ({order.customerEmail})
                </p>
            </div>
        </div>
    );
}
