"use client";

import { useState } from "react";
import { ChevronLeft, ChevronRight } from "lucide-react";
import type { AwardItem } from "@/lib/techc";

/**
 * Awards as a printed ledger.
 *
 * A numbered register on the left drives a single photographic plate on the
 * right — closer to a catalogue of results than a card carousel. Arrow keys and
 * the row list both change the selected entry.
 */
export function AwardLedger({ items }: { items: AwardItem[] }) {
  const [active, setActive] = useState(0);
  const item = items[active];

  const step = (delta: number) =>
    setActive((i) => (i + delta + items.length) % items.length);

  return (
    <div className="grid gap-8 lg:grid-cols-[0.95fr_1.05fr] lg:gap-14">
      {/* Register */}
      <div>
        <div className="flex items-baseline justify-between border-b border-doc-ink pb-2">
          <span className="label text-doc-ink">Daftar Prestasi</span>
          <span className="num text-[0.6875rem] text-doc-ink-3">
            {items.length} entri · 2025
          </span>
        </div>

        <ul>
          {items.map((a, i) => {
            const on = i === active;
            return (
              <li key={a.title}>
                <button
                  type="button"
                  onClick={() => setActive(i)}
                  aria-current={on ? "true" : undefined}
                  className={`grid w-full grid-cols-[2.25rem_1fr_auto] items-baseline gap-3 border-b border-doc-line py-3.5 text-left transition-colors ${
                    on ? "bg-doc-signal-tint/60" : "hover:bg-doc-paper-2/70"
                  }`}
                >
                  <span
                    className={`num pl-1 text-[0.6875rem] ${
                      on ? "font-semibold text-doc-signal" : "text-doc-ink-3"
                    }`}
                  >
                    {String(i + 1).padStart(2, "0")}
                  </span>
                  <span className="min-w-0">
                    <span
                      className={`block truncate text-[0.875rem] leading-snug ${
                        on ? "font-semibold text-doc-ink" : "text-doc-ink-2"
                      }`}
                    >
                      {a.title}
                    </span>
                    <span className="label mt-1 block truncate">{a.team}</span>
                  </span>
                  <span
                    className={`num pr-1 text-[0.625rem] uppercase tracking-wider ${
                      on ? "text-doc-signal" : "text-doc-ink-3"
                    }`}
                  >
                    {a.rank}
                  </span>
                </button>
              </li>
            );
          })}
        </ul>

        <div className="mt-4 flex items-center gap-2">
          <button
            type="button"
            onClick={() => step(-1)}
            aria-label="Prestasi sebelumnya"
            className="grid size-9 place-items-center border border-doc-line text-doc-ink transition-colors hover:border-doc-ink hover:bg-doc-ink hover:text-doc-paper"
          >
            <ChevronLeft className="size-4" />
          </button>
          <button
            type="button"
            onClick={() => step(1)}
            aria-label="Prestasi berikutnya"
            className="grid size-9 place-items-center border border-doc-line text-doc-ink transition-colors hover:border-doc-ink hover:bg-doc-ink hover:text-doc-paper"
          >
            <ChevronRight className="size-4" />
          </button>
          <span className="num ml-1 text-[0.6875rem] text-doc-ink-3">
            {String(active + 1).padStart(2, "0")}/{String(items.length).padStart(2, "0")}
          </span>
        </div>
      </div>

      {/* Plate */}
      <figure className="lg:sticky lg:top-20 lg:self-start">
        <div className="zoom-parent relative aspect-square overflow-hidden border border-doc-line bg-doc-paper-2">
          {/* eslint-disable-next-line @next/next/no-img-element */}
          <img
            key={item.image}
            src={item.image}
            alt={item.title}
            loading="lazy"
            className="fade-in plate-img absolute inset-0 size-full object-contain"
          />
        </div>

        <figcaption className="mt-4 border-t border-doc-line pt-4">
          <div className="flex items-baseline gap-3">
            <span className="num text-[0.6875rem] font-semibold text-doc-signal">
              {String(active + 1).padStart(2, "0")}
            </span>
            <h3 className="display text-[1.05rem] text-doc-ink">{item.title}</h3>
          </div>
          <p className="mt-3 max-w-[52ch] text-[0.875rem] leading-7 text-doc-ink-2">{item.desc}</p>
          <dl className="mt-4 flex flex-wrap gap-x-8 gap-y-2">
            <div>
              <dt className="label">Tim</dt>
              <dd className="mt-0.5 text-[0.8125rem] text-doc-ink">{item.team}</dd>
            </div>
            <div>
              <dt className="label">Tahun</dt>
              <dd className="num mt-0.5 text-[0.8125rem] text-doc-ink">{item.year}</dd>
            </div>
            <div>
              <dt className="label">Peringkat</dt>
              <dd className="num mt-0.5 text-[0.8125rem] text-doc-ink">{item.rank}</dd>
            </div>
          </dl>
        </figcaption>
      </figure>
    </div>
  );
}
