"use client";

import { useMemo, useState } from "react";
import Link from "next/link";
import { useParams } from "next/navigation";
import { CATEGORIES, PARTS } from "@/data/gunbuilderParts";
import type { CategoryId } from "@/types/gunbuilder";

type ViewMode = "card" | "list";

export default function CategoryPage() {
  const params = useParams();
  const categoryId = params.categoryId as CategoryId;
  const [viewMode, setViewMode] = useState<ViewMode>("list");

  const category = useMemo(
    () => CATEGORIES.find((c) => c.id === categoryId),
    [categoryId],
  );

  const parts = useMemo(
    () => PARTS.filter((p) => p.categoryId === categoryId),
    [categoryId],
  );

  if (!category) {
    return (
      <main className="min-h-screen bg-black text-zinc-50">
        <div className="mx-auto max-w-6xl px-4 py-6 lg:py-10">
          <div className="text-center">
            <h1 className="text-2xl font-semibold text-zinc-50 mb-4">
              Category Not Found
            </h1>
            <Link
              href="/gunbuilder"
              className="text-amber-300 hover:text-amber-200 underline"
            >
              Return to Gunbuilder
            </Link>
          </div>
        </div>
      </main>
    );
  }

  return (
    <main className="min-h-screen bg-black text-zinc-50">
      <div className="mx-auto max-w-6xl px-4 py-6 lg:py-10">
        {/* Header */}
        <header className="mb-6">
          <Link
            href="/gunbuilder"
            className="text-xs text-zinc-400 hover:text-zinc-300 mb-4 inline-block"
          >
            ← Back to Gunbuilder
          </Link>
          <div className="flex items-start justify-between gap-4">
            <div>
              <p className="text-xs font-semibold tracking-[0.2em] uppercase text-zinc-500">
                Shadow Standard
              </p>
              <h1 className="mt-1 text-2xl md:text-3xl font-semibold tracking-tight">
                {category.name} <span className="text-amber-300">Parts</span>
              </h1>
              <p className="mt-2 text-sm text-zinc-400 max-w-xl">
                Browse all available {category.name.toLowerCase()} options for your
                build.
              </p>
            </div>
            {parts.length > 0 && (
              <div className="flex gap-2 border border-zinc-800 rounded-md p-1 bg-zinc-950/60">
                <button
                  type="button"
                  onClick={() => setViewMode("card")}
                  className={`px-3 py-1.5 text-xs font-medium rounded transition-colors ${
                    viewMode === "card"
                      ? "bg-zinc-800 text-zinc-50"
                      : "text-zinc-400 hover:text-zinc-300"
                  }`}
                  aria-label="Card view"
                >
                  Card
                </button>
                <button
                  type="button"
                  onClick={() => setViewMode("list")}
                  className={`px-3 py-1.5 text-xs font-medium rounded transition-colors ${
                    viewMode === "list"
                      ? "bg-zinc-800 text-zinc-50"
                      : "text-zinc-400 hover:text-zinc-300"
                  }`}
                  aria-label="List view"
                >
                  List
                </button>
              </div>
            )}
          </div>
        </header>

        {/* Parts Grid/List */}
        <section className="rounded-lg border border-zinc-800 bg-zinc-950/60 p-3 md:p-4">
          {parts.length === 0 ? (
            <p className="text-sm text-zinc-500 text-center py-8">
              No parts available for this category yet.
            </p>
          ) : viewMode === "card" ? (
            <div className="grid gap-3 md:grid-cols-2 lg:grid-cols-3">
              {parts.map((part) => (
                <div
                  key={part.id}
                  className="group relative border border-zinc-700 rounded-md p-3 bg-zinc-900/50 hover:border-amber-400/60 hover:bg-amber-400/10 transition-all duration-200 flex flex-col"
                >
                  <Link
                    href={`/gunbuilder?select=${categoryId}:${part.id}`}
                    className="flex-1"
                  >
                    <div className="flex justify-between items-center gap-2 mb-3">
                      <div className="flex-1">
                        <div className="text-sm font-semibold text-zinc-50">
                          {part.brand} <span className="font-normal">— {part.name}</span>
                        </div>
                        {part.notes && (
                          <p className="mt-1 text-xs text-zinc-400 line-clamp-2">
                            {part.notes}
                          </p>
                        )}
                      </div>
                      <div className="text-sm font-semibold text-amber-300 whitespace-nowrap">
                        ${part.price.toFixed(2)}
                      </div>
                    </div>
                  </Link>
                  <Link
                    href={`/gunbuilder/${categoryId}/${part.id}`}
                    className="w-full rounded-md border border-zinc-700 bg-zinc-800/50 px-3 py-2 text-xs font-medium text-zinc-300 hover:bg-zinc-700 hover:border-zinc-600 transition-colors text-center"
                  >
                    View Details
                  </Link>
                  {/* Hover Overlay */}
                  <div className="absolute inset-0 rounded-md bg-amber-400/10 opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none flex items-center justify-center">
                    <div className="text-sm font-semibold text-amber-200 tracking-wide">
                      Add to Build
                    </div>
                  </div>
                </div>
              ))}
            </div>
          ) : (
            <div className="space-y-2">
              {parts.map((part) => (
                <div
                  key={part.id}
                  className="group relative border border-zinc-700 rounded-md p-3 bg-zinc-900/50 hover:border-amber-400/60 hover:bg-amber-400/10 transition-all duration-200"
                >
                  <div className="flex items-center justify-between gap-4">
                    <div className="flex-1 min-w-0">
                      <Link
                        href={`/gunbuilder?select=${categoryId}:${part.id}`}
                        className="block"
                      >
                        <div className="text-sm font-semibold text-zinc-50">
                          {part.brand} <span className="font-normal">— {part.name}</span>
                        </div>
                        {part.notes && (
                          <p className="mt-1 text-xs text-zinc-400 line-clamp-1">
                            {part.notes}
                          </p>
                        )}
                      </Link>
                    </div>
                    <div className="flex items-center gap-3">
                      <div className="text-sm font-semibold text-amber-300 whitespace-nowrap">
                        ${part.price.toFixed(2)}
                      </div>
                      <Link
                        href={`/gunbuilder/${categoryId}/${part.id}`}
                        className="rounded-md border border-zinc-700 bg-zinc-800/50 px-3 py-1.5 text-xs font-medium text-zinc-300 hover:bg-zinc-700 hover:border-zinc-600 transition-colors whitespace-nowrap"
                      >
                        View Details
                      </Link>
                    </div>
                  </div>
                  {/* Hover Overlay */}
                  <div className="absolute inset-0 rounded-md bg-amber-400/10 opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none flex items-center justify-center">
                    <div className="text-sm font-semibold text-amber-200 tracking-wide">
                      Add to Build
                    </div>
                  </div>
                </div>
              ))}
            </div>
          )}
        </section>
      </div>
    </main>
  );
}

