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

import { ConfigurableTable } from '@/components/ui/configurable-table';
import { PaginationLinks } from '@/components/ui/pagination-links';
import { PublicLayout } from '@/layouts/public-layout';
import type { PaginatedData } from '@/types';

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

type OrdersIndexProps = {
    orders: PaginatedData<OrderRow>;
};

export default function AccountOrdersIndex({ orders }: OrdersIndexProps) {
    return (
        <PublicLayout title="My Orders">
            <Head title="My Orders" />

            <div className="space-y-6">
                <div className="flex flex-wrap items-center justify-between gap-3">
                    <h1 className="text-2xl font-semibold text-main">
                        My Orders
                    </h1>
                    <Link
                        href="/catalog"
                        className="rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted hover:bg-surface hover:text-main"
                    >
                        Continue Shopping
                    </Link>
                </div>

                {orders.data.length === 0 ? (
                    <div className="rounded-2xl border border-dashed border-border bg-card p-10 text-center text-sm text-muted">
                        You have not placed any orders yet.
                    </div>
                ) : (
                    <>
                        <ConfigurableTable
                            tableId="account-orders"
                            className="hidden overflow-x-auto rounded-xl border border-border md:block"
                        >
                            <table className="min-w-full divide-y divide-border text-sm">
                                <thead className="bg-surface">
                                    <tr>
                                        <th className="px-4 py-3 text-left font-medium text-muted">
                                            Order
                                        </th>
                                        <th className="px-4 py-3 text-left font-medium text-muted">
                                            Status
                                        </th>
                                        <th className="px-4 py-3 text-left font-medium text-muted">
                                            Items
                                        </th>
                                        <th className="px-4 py-3 text-left font-medium text-muted">
                                            Total
                                        </th>
                                        <th className="px-4 py-3 text-left font-medium text-muted">
                                            Placed
                                        </th>
                                        <th className="px-4 py-3 text-left font-medium text-muted">
                                            Action
                                        </th>
                                    </tr>
                                </thead>
                                <tbody className="divide-y divide-border bg-card">
                                    {orders.data.map((order) => (
                                        <tr key={order.id}>
                                            <td className="px-4 py-3 font-medium text-main">
                                                {order.order_number}
                                            </td>
                                            <td className="px-4 py-3 text-muted">
                                                {order.status}
                                            </td>
                                            <td className="px-4 py-3 text-muted">
                                                {order.items_count}
                                            </td>
                                            <td className="px-4 py-3 text-muted">
                                                {formatMoney(
                                                    order.total_minor,
                                                    order.currency,
                                                )}
                                            </td>
                                            <td className="px-4 py-3 text-muted">
                                                {order.placed_at || '-'}
                                            </td>
                                            <td className="px-4 py-3">
                                                <Link
                                                    href={`/account/orders/${order.id}`}
                                                    className="rounded-md border border-border px-2.5 py-1 text-xs font-medium text-muted hover:bg-surface hover:text-main"
                                                >
                                                    View
                                                </Link>
                                            </td>
                                        </tr>
                                    ))}
                                </tbody>
                            </table>
                        </ConfigurableTable>

                        <div className="space-y-3 md:hidden">
                            {orders.data.map((order) => (
                                <article
                                    key={order.id}
                                    className="rounded-xl border border-border bg-card p-4"
                                >
                                    <p className="font-semibold text-main">
                                        {order.order_number}
                                    </p>
                                    <p className="text-xs text-muted">
                                        {order.placed_at || '-'}
                                    </p>
                                    <div className="mt-2 grid grid-cols-2 gap-2 text-xs text-muted">
                                        <p>Status: {order.status}</p>
                                        <p>Items: {order.items_count}</p>
                                        <p>
                                            {formatMoney(
                                                order.total_minor,
                                                order.currency,
                                            )}
                                        </p>
                                    </div>
                                    <Link
                                        href={`/account/orders/${order.id}`}
                                        className="mt-3 inline-flex rounded-md border border-border px-2.5 py-1 text-xs font-medium text-muted hover:bg-surface hover:text-main"
                                    >
                                        View Order
                                    </Link>
                                </article>
                            ))}
                        </div>

                        <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
                            <p className="text-sm text-muted">
                                Showing {orders.from || 0} to {orders.to || 0}{' '}
                                of {orders.total} orders
                            </p>
                            <PaginationLinks links={orders.links} />
                        </div>
                    </>
                )}
            </div>
        </PublicLayout>
    );
}

function formatMoney(minor: number, currency: string): string {
    return `${currency} ${(minor / 100).toFixed(2)}`;
}
