"use client";

import Link from "next/link";
import { useEffect, useState } from "react";
import { getWishlist, removeWishlist, type WishItem } from "@/lib/wishlist";
import { addToCart } from "@/lib/cart";

export default function WishlistPage() {
  const [items, setItems] = useState<WishItem[]>([]);
  const [ready, setReady] = useState(false);

  useEffect(() => {
    const sync = () => setItems(getWishlist());
    sync();
    setReady(true);
    window.addEventListener("awafi-wish-change", sync);
    window.addEventListener("storage", sync);
    return () => {
      window.removeEventListener("awafi-wish-change", sync);
      window.removeEventListener("storage", sync);
    };
  }, []);

  return (
    <>
      <section className="pb-cathead pb-tone-rose">
        <div className="pb-shell">
          <nav className="pb-crumbs" aria-label="Breadcrumb">
            <Link href="/portal">Home</Link><span>/</span><span className="pb-crumbs-current">Wishlist</span>
          </nav>
          <h1 className="pb-cathead-title">Your wishlist</h1>
          <p className="pb-cathead-sub">Products you&apos;ve saved for later.</p>
        </div>
      </section>

      <div className="pb-shell">
        {ready && items.length > 0 ? (
          <div className="pb-grid">
            {items.map((p) => {
              const disc = p.oldPrice ? Math.round((1 - p.price / p.oldPrice) * 100) : 0;
              return (
                <div className="pb-grid-cell" key={p.slug}>
                  <article className="pb-card">
                    <Link href={`/portal/product/${p.slug}`} className="pb-card-media">
                      {disc > 0 && <span className="pb-badge pb-badge--sale">-{disc}%</span>}
                      {/* eslint-disable-next-line @next/next/no-img-element */}
                      <img className="pb-card-img" src={p.image} alt={`${p.brand} ${p.name}`} loading="lazy" />
                    </Link>
                    <button type="button" className="pb-wish is-wished" onClick={() => removeWishlist(p.slug)} aria-label="Remove from wishlist">
                      <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M12 20s-7-4.4-9.3-8.4C1 8.5 2.4 5 5.8 5 8 5 12 7.5 12 7.5S16 5 18.2 5C21.6 5 23 8.5 21.3 11.6 19 15.6 12 20 12 20Z" /></svg>
                    </button>
                    <div className="pb-card-body">
                      <span className="pb-card-brand">{p.brand}</span>
                      <Link href={`/portal/product/${p.slug}`} className="pb-card-name">{p.name}</Link>
                      <div className="pb-card-foot">
                        <div className="pb-price">
                          <span className="pb-price-now">AED {p.price}</span>
                          {p.oldPrice && p.oldPrice > p.price && <span className="pb-price-was">AED {p.oldPrice}</span>}
                        </div>
                        <button className="pb-card-add" onClick={() => addToCart({ id: p.slug, name: p.name, brand: p.brand, price: p.price, image: p.image, qty: 1 })} aria-label={`Add ${p.name} to cart`}>
                          <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2"><path d="M12 5v14M5 12h14" strokeLinecap="round" /></svg>
                        </button>
                      </div>
                    </div>
                  </article>
                </div>
              );
            })}
          </div>
        ) : (
          <div className="pb-empty">
            <svg viewBox="0 0 24 24" width="46" height="46" fill="none" stroke="currentColor" strokeWidth="1.4" style={{ color: "var(--line)", margin: "0 auto 14px" }}><path d="M12 20s-7-4.4-9.3-8.4C1 8.5 2.4 5 5.8 5 8 5 12 7.5 12 7.5S16 5 18.2 5C21.6 5 23 8.5 21.3 11.6 19 15.6 12 20 12 20Z" /></svg>
            <p>Your wishlist is empty.</p>
            <Link href="/portal#categories" className="pb-btn pb-btn--primary">Browse products</Link>
          </div>
        )}
      </div>

      <div className="pb-section-spacer" />
    </>
  );
}
