import { notFound } from 'next/navigation';
import { prisma } from '@/lib/prisma';
import AdminNav from '@/components/AdminNav';
import { archiveOrder, unarchiveOrder } from '../actions';

function formatPrice(cents: number) {
  return `£${(cents / 100).toFixed(2)}`;
}

export default async function OrderDetailPage({ params }: { params: { id: string } }) {
  const order = await prisma.order.findUnique({
    where: { id: params.id },
    include: { items: true },
  });
  if (!order) notFound();

  const shipping = order.shippingAddress ? JSON.parse(order.shippingAddress) : null;

  return (
    <div className="mx-auto max-w-3xl px-6 py-12">
      <AdminNav />
      <div className="flex items-baseline justify-between gap-3">
        <h1 className="font-display text-3xl">Order</h1>
        <div className="flex items-center gap-3">
          {order.archived && <span className="tag-label">Archived</span>}
          <a
            href={`/api/orders/${order.id}/design-spec`}
            className="tag-label border border-line px-3 py-1.5 hover:border-clay hover:text-clay"
          >
            ↓ Download specs
          </a>
          <form action={order.archived ? unarchiveOrder : archiveOrder}>
            <input type="hidden" name="id" value={order.id} />
            <button type="submit" className="tag-label border border-line px-3 py-1.5 hover:border-clay hover:text-clay">
              {order.archived ? 'Unarchive' : 'Archive'}
            </button>
          </form>
          <span
            className={`tag-label rounded-full px-3 py-1 ${
              order.status === 'PAID'
                ? 'bg-splash-blue/10 text-splash-blue'
                : order.status === 'FAILED'
                  ? 'bg-splash-pink/10 text-splash-pink'
                  : 'bg-splash-orange/10 text-splash-orange'
            }`}
          >
            {order.status}
          </span>
        </div>
      </div>

      <div className="mt-6 grid gap-6 border border-line bg-surface p-6 sm:grid-cols-2">
        <div>
          <p className="tag-label mb-1">Customer</p>
          <p className="text-sm">{order.email ?? 'Not yet available'}</p>
        </div>
        <div>
          <p className="tag-label mb-1">Placed</p>
          <p className="text-sm">
            {new Date(order.createdAt).toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' })}
          </p>
        </div>
        {shipping && (
          <div className="sm:col-span-2">
            <p className="tag-label mb-1">Shipping address</p>
            <p className="text-sm">
              {shipping.name}
              <br />
              {[shipping.address?.line1, shipping.address?.line2].filter(Boolean).join(', ')}
              <br />
              {[shipping.address?.city, shipping.address?.postal_code].filter(Boolean).join(', ')}
              <br />
              {shipping.address?.country}
            </p>
          </div>
        )}
      </div>

      <div className="mt-10 divide-y divide-line border-t border-line">
        {order.items.map((item) => {
          const design = JSON.parse(item.designJson) as { front: unknown[]; back: unknown[] };
          const hasFrontDesign = design.front.length > 0;
          const hasBackDesign = design.back.length > 0;
          const frontPlacement = item.placementPreviewUrl || item.designPreviewUrl;
          return (
            <div key={item.id} className="py-8">
              <div className="flex flex-wrap items-baseline justify-between gap-2">
                <p className="font-display text-xl">{item.productName}</p>
                <p className="text-sm text-muted">
                  {item.color}
                  {item.size ? `, ${item.size}` : ''} · qty {item.quantity} ·{' '}
                  {formatPrice(item.unitPrice * item.quantity)}
                </p>
              </div>

              <div className="mt-4 grid gap-6 sm:grid-cols-2">
                <div>
                  <img
                    src={frontPlacement || undefined}
                    alt={`${item.productName} — where the design sits`}
                    className="w-full max-w-md border border-line bg-paper object-contain"
                  />
                  <div className="mt-2 flex items-center justify-between">
                    <p className="tag-label">{item.placementPreviewUrl ? 'On product — front' : 'Front'}</p>
                    {hasFrontDesign && (
                      <a
                        href={item.designPreviewUrl}
                        download={`order-${order.id}-${item.id}-front.png`}
                        className="text-xs text-clay hover:text-clay-dark"
                      >
                        Download print file
                      </a>
                    )}
                  </div>
                </div>

                {item.placementPreviewUrlBack && (
                  <div>
                    <img
                      src={item.placementPreviewUrlBack}
                      alt={`${item.productName} back — where the design sits`}
                      className="w-full max-w-md border border-line bg-paper object-contain"
                    />
                    <div className="mt-2 flex items-center justify-between">
                      <p className="tag-label">On product — back</p>
                      {hasBackDesign && item.designPreviewUrlBack && (
                        <a
                          href={item.designPreviewUrlBack}
                          download={`order-${order.id}-${item.id}-back.png`}
                          className="text-xs text-clay hover:text-clay-dark"
                        >
                          Download print file
                        </a>
                      )}
                    </div>
                  </div>
                )}
              </div>

              {(hasFrontDesign || hasBackDesign) && (
                <div className="mt-6 space-y-4 border-t border-line pt-4">
                  <p className="tag-label">Design details</p>
                  {hasFrontDesign && design.front.length > 0 && (
                    <div>
                      <p className="text-sm font-medium">Front design elements:</p>
                      <div className="mt-2 space-y-2 text-xs">
                        {(design.front as any[]).map((el, idx) => (
                          <div key={idx} className="border border-line bg-surface p-2">
                            {el.type === 'text' ? (
                              <div>
                                <p className="font-mono">
                                  Text: "{el.text}"
                                </p>
                                <p className="text-muted">
                                  Font: {el.fontFamily} {el.fontSize}px · Color: {el.fill}
                                </p>
                                <p className="text-muted">
                                  Position: x={Math.round(el.x)}px, y={Math.round(el.y)}px · Rotation: {Math.round(el.rotation)}°
                                </p>
                              </div>
                            ) : (
                              <div>
                                <p className="font-mono">Image</p>
                                <p className="text-muted">
                                  Size: {Math.round(el.width)}px × {Math.round(el.height)}px
                                </p>
                                <p className="text-muted">
                                  Position: x={Math.round(el.x)}px, y={Math.round(el.y)}px · Rotation: {Math.round(el.rotation)}°
                                </p>
                              </div>
                            )}
                          </div>
                        ))}
                      </div>
                    </div>
                  )}
                  {hasBackDesign && design.back.length > 0 && (
                    <div>
                      <p className="text-sm font-medium">Back design elements:</p>
                      <div className="mt-2 space-y-2 text-xs">
                        {(design.back as any[]).map((el, idx) => (
                          <div key={idx} className="border border-line bg-surface p-2">
                            {el.type === 'text' ? (
                              <div>
                                <p className="font-mono">
                                  Text: "{el.text}"
                                </p>
                                <p className="text-muted">
                                  Font: {el.fontFamily} {el.fontSize}px · Color: {el.fill}
                                </p>
                                <p className="text-muted">
                                  Position: x={Math.round(el.x)}px, y={Math.round(el.y)}px · Rotation: {Math.round(el.rotation)}°
                                </p>
                              </div>
                            ) : (
                              <div>
                                <p className="font-mono">Image</p>
                                <p className="text-muted">
                                  Size: {Math.round(el.width)}px × {Math.round(el.height)}px
                                </p>
                                <p className="text-muted">
                                  Position: x={Math.round(el.x)}px, y={Math.round(el.y)}px · Rotation: {Math.round(el.rotation)}°
                                </p>
                              </div>
                            )}
                          </div>
                        ))}
                      </div>
                    </div>
                  )}
                </div>
              )}
            </div>
          );
        })}
      </div>

      <div className="mt-6 flex justify-end">
        <div className="flex items-baseline gap-3">
          <span className="tag-label">Total</span>
          <span className="font-mono text-xl">{formatPrice(order.total)}</span>
        </div>
      </div>
    </div>
  );
}
