"use client";

import Image from "next/image";
import Link from "next/link";

import {
  ArrowLeft,
  Minus,
  Plus,
  ShoppingCart,
  Trash2,
} from "lucide-react";

import {
  formatPrice,
  useCart,
} from "@/lib/cart";

export default function PanierPage() {
  const {
    lines,
    add,
    remove,
    setQty,
    total,
  } = useCart();

  return (
    <main className="min-h-screen bg-[#faf8f4] text-[#1c1c1c]">
      <section className="bg-[#151515] px-6 py-12 text-white">
        <div className="mx-auto max-w-7xl">
          <Link
            href="/menu"
            className="mb-6 inline-flex items-center gap-2 text-sm text-white/70 transition hover:text-white"
          >
            <ArrowLeft size={18} />
            Retour au menu
          </Link>

          <p className="mb-3 text-sm font-semibold uppercase tracking-[0.3em] text-[#d6a85f]">
            Happy Hours
          </p>

          <h1 className="text-4xl font-bold md:text-5xl">
            Votre panier
          </h1>

          <p className="mt-4 text-white/60">
            Vérifiez votre commande avant de continuer.
          </p>
        </div>
      </section>

      <section className="mx-auto max-w-7xl px-6 py-12">
        {lines.length === 0 ? (
          <div className="rounded-3xl bg-white px-6 py-20 text-center shadow-sm">
            <ShoppingCart
              size={64}
              className="mx-auto mb-6 text-gray-300"
            />

            <h2 className="text-2xl font-bold">
              Votre panier est vide
            </h2>

            <p className="mx-auto mt-3 max-w-md text-gray-500">
              Découvrez notre menu et ajoutez vos plats préférés à votre panier.
            </p>

            <Link
              href="/menu"
              className="mt-8 inline-flex rounded-full bg-[#151515] px-8 py-4 font-semibold text-white transition hover:bg-[#333]"
            >
              Découvrir le menu
            </Link>
          </div>
        ) : (
          <div className="grid gap-10 lg:grid-cols-[1fr_380px]">
            <div className="space-y-5">
              {lines.map(
                ({
                  item,
                  qty,
                }) => (
                  <article
                    key={item.id}
                    className="flex flex-col gap-5 rounded-3xl bg-white p-5 shadow-sm sm:flex-row sm:items-center"
                  >
                    <div className="relative h-32 w-full overflow-hidden rounded-2xl bg-gray-100 sm:w-32">
                      <Image
                        src={
                          item.image?.trim() ||
                          "/placeholder.svg"
                        }
                        alt={item.name}
                        fill
                        sizes="128px"
                        className="object-cover"
                      />
                    </div>

                    <div className="flex-1">
                      <h2 className="text-xl font-bold">
                        {item.name}
                      </h2>

                      <p className="mt-2 text-sm text-gray-500">
                        {formatPrice(
                          item.price
                        )}{" "}
                        FCFA / unité
                      </p>

                      <p className="mt-2 font-bold text-[#b8863b]">
                        {formatPrice(
                          item.price * qty
                        )}{" "}
                        FCFA
                      </p>
                    </div>

                    <div className="flex items-center justify-between gap-4 sm:flex-col sm:items-end">
                      <div className="flex items-center gap-2">
                        <button
                          type="button"
                          onClick={() =>
                            setQty(
                              item.id,
                              qty - 1
                            )
                          }
                          aria-label={`Diminuer la quantité de ${item.name}`}
                          className="flex h-9 w-9 items-center justify-center rounded-full bg-gray-100 transition hover:bg-gray-200"
                        >
                          <Minus size={16} />
                        </button>

                        <span className="w-8 text-center font-bold">
                          {qty}
                        </span>

                        <button
                          type="button"
                          onClick={() =>
                            add(item)
                          }
                          disabled={
                            qty >= 99
                          }
                          aria-label={`Ajouter une unité de ${item.name}`}
                          className="flex h-9 w-9 items-center justify-center rounded-full bg-gray-100 transition hover:bg-gray-200 disabled:cursor-not-allowed disabled:opacity-40"
                        >
                          <Plus size={16} />
                        </button>
                      </div>

                      <button
                        type="button"
                        onClick={() =>
                          remove(item.id)
                        }
                        className="flex items-center gap-2 text-sm font-semibold text-red-500 transition hover:text-red-700"
                      >
                        <Trash2 size={16} />
                        Supprimer
                      </button>
                    </div>
                  </article>
                )
              )}
            </div>

            <aside className="h-fit rounded-3xl bg-white p-7 shadow-sm lg:sticky lg:top-6">
              <h2 className="text-2xl font-bold">
                Résumé de la commande
              </h2>

              <div className="mt-8 space-y-5">
                <div className="flex justify-between text-gray-600">
                  <span>Sous-total</span>

                  <span className="font-semibold">
                    {formatPrice(
                      total
                    )}{" "}
                    FCFA
                  </span>
                </div>

                <div className="flex justify-between gap-4 text-gray-600">
                  <span>Livraison</span>

                  <span className="text-right font-semibold">
                    Calculée selon votre zone
                  </span>
                </div>

                <div className="border-t pt-5">
                  <div className="flex items-center justify-between">
                    <span className="text-lg font-bold">
                      Total estimé
                    </span>

                    <span className="text-2xl font-bold text-[#b8863b]">
                      {formatPrice(
                        total
                      )}{" "}
                      FCFA
                    </span>
                  </div>
                </div>
              </div>

              <Link
                href="/checkout"
                className="mt-8 block w-full rounded-full bg-[#151515] px-6 py-4 text-center font-bold text-white transition hover:bg-[#333]"
              >
                Continuer la commande
              </Link>

              <Link
                href="/menu"
                className="mt-4 block text-center text-sm font-semibold text-gray-500 transition hover:text-[#151515]"
              >
                Continuer mes achats
              </Link>
            </aside>
          </div>
        )}
      </section>
    </main>
  );
}