import { Head, Link, router, usePage } from '@inertiajs/react';

import { ConfigurableTable } from '@/components/ui/configurable-table';
import { PaginationLinks } from '@/components/ui/pagination-links';
import { AdminLayout } from '@/layouts/admin-layout';
import { formatMoney } from '@/lib/money';
import type { AppPageProps, PaginatedData } from '@/types';

type Customer = {
    id: number;
    name: string;
    email: string;
    phone: string | null;
    address_line1: string | null;
    address_line2: string | null;
    city: string | null;
    postal_code: string | null;
    country: string | null;
    orders_count: number;
    orders_total_minor: number;
    created_at: string | null;
};

type OrderRow = {
    id: number;
    order_number: string;
    status: string;
    status_label: string;
    currency: string;
    total_minor: number;
    items_count: number;
    placed_at: string | null;
};

type CustomerShowProps = {
    customer: Customer;
    orders: PaginatedData<OrderRow>;
};

export default function AdminCustomerShow({
    customer,
    orders,
}: CustomerShowProps) {
    const { config } = usePage<AppPageProps>().props;

    return (
        <AdminLayout>
            <Head title={customer.name} />

            <div className="space-y-5">
                <div className="flex flex-wrap items-start justify-between gap-3">
                    <div>
                        <Link
                            href="/admin/customers"
                            className="text-sm font-medium text-slate-600 hover:text-[#0a1530] dark:text-[#d7c7ff] dark:hover:text-[#efe8ff]"
                        >
                            Back to customers
                        </Link>
                        <h1 className="mt-2 text-2xl font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                            {customer.name}
                        </h1>
                        <p className="mt-1 text-sm text-slate-600 dark:text-[#c7bde8]">
                            {customer.email}
                        </p>
                    </div>
                    <div className="flex flex-wrap items-center gap-2">
                        <Link
                            href={`/admin/customers/${customer.id}/edit`}
                            className="rounded-lg border border-slate-300 px-3 py-2 text-sm font-medium text-slate-700 hover:bg-slate-100 dark:border-[#4a3a78] dark:text-[#e5dcff] dark:hover:bg-[#2a1d50]"
                        >
                            Edit
                        </Link>
                        <button
                            type="button"
                            onClick={() => deleteCustomer(customer.id)}
                            className="rounded-lg border border-rose-200 px-3 py-2 text-sm font-medium text-rose-700 hover:bg-rose-50 dark:border-rose-400/40 dark:text-rose-200 dark:hover:bg-rose-400/10"
                        >
                            Delete
                        </button>
                        <div className="rounded-xl border border-slate-200 bg-slate-50 px-4 py-3 text-sm text-[#0a1530] dark:border-[#3c2b68] dark:bg-[#160f2d] dark:text-[#efe8ff]">
                            <p className="font-semibold">
                                {customer.orders_count} orders
                            </p>
                            <p className="text-slate-600 dark:text-[#c7bde8]">
                                {formatMoney(
                                    customer.orders_total_minor,
                                    config.default_currency,
                                )}
                            </p>
                        </div>
                    </div>
                </div>

                <section className="grid gap-4 lg:grid-cols-3">
                    <InfoPanel title="Contact">
                        <p>{customer.name}</p>
                        <p>{customer.email}</p>
                        {customer.phone ? <p>{customer.phone}</p> : null}
                    </InfoPanel>
                    <InfoPanel title="Address">
                        <p>{customer.address_line1 || '-'}</p>
                        {customer.address_line2 ? (
                            <p>{customer.address_line2}</p>
                        ) : null}
                        <p>
                            {[customer.city, customer.postal_code]
                                .filter(Boolean)
                                .join(', ') || '-'}
                        </p>
                        <p>{customer.country || '-'}</p>
                    </InfoPanel>
                    <InfoPanel title="Account">
                        <p>Customer ID: {customer.id}</p>
                        <p>Joined: {customer.created_at || '-'}</p>
                    </InfoPanel>
                </section>

                <section className="space-y-3">
                    <h2 className="text-lg font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                        Order History
                    </h2>
                    <ConfigurableTable
                        tableId="admin-customers-show"
                        className="overflow-x-auto rounded-xl border border-slate-200 dark:border-[#3c2b68]"
                    >
                        <table className="min-w-full divide-y divide-slate-200 text-sm dark:divide-[#3c2b68]">
                            <thead className="bg-slate-50 dark:bg-[#2a1d50]/70">
                                <tr>
                                    <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                        Order
                                    </th>
                                    <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                        Status
                                    </th>
                                    <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                        Items
                                    </th>
                                    <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                        Total
                                    </th>
                                    <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                        Actions
                                    </th>
                                </tr>
                            </thead>
                            <tbody className="divide-y divide-slate-100 bg-white dark:divide-[#3c2b68] dark:bg-[#211740]">
                                {orders.data.map((order) => (
                                    <tr key={order.id}>
                                        <td className="px-4 py-3">
                                            <p className="font-medium text-[#0a1530] dark:text-[#efe8ff]">
                                                {order.order_number}
                                            </p>
                                            <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                                {order.placed_at || '-'}
                                            </p>
                                        </td>
                                        <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                            {order.status_label}
                                        </td>
                                        <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                            {order.items_count}
                                        </td>
                                        <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                            {formatMoney(
                                                order.total_minor,
                                                order.currency,
                                            )}
                                        </td>
                                        <td className="px-4 py-3">
                                            <Link
                                                href={`/admin/orders/${order.id}`}
                                                className="rounded-md border border-slate-300 px-2.5 py-1 text-xs font-medium text-slate-700 hover:bg-slate-100 dark:border-[#4a3a78] dark:text-[#e5dcff] dark:hover:bg-[#2a1d50]"
                                            >
                                                View
                                            </Link>
                                        </td>
                                    </tr>
                                ))}
                            </tbody>
                        </table>
                    </ConfigurableTable>
                    <PaginationLinks links={orders.links} />
                </section>
            </div>
        </AdminLayout>
    );
}

function deleteCustomer(id: number) {
    if (!window.confirm('Delete this customer?')) {
        return;
    }

    router.delete(`/admin/customers/${id}`);
}

function InfoPanel({
    title,
    children,
}: {
    title: string;
    children: React.ReactNode;
}) {
    return (
        <div className="rounded-xl border border-slate-200 bg-white p-4 dark:border-[#3c2b68] dark:bg-[#211740]">
            <h2 className="text-base font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                {title}
            </h2>
            <div className="mt-3 space-y-1 text-sm text-slate-700 dark:text-[#e5dcff]">
                {children}
            </div>
        </div>
    );
}
