'use client';

import { FolderOpen } from 'lucide-react';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { toast } from 'sonner';

interface Collection {
  id: string;
  title: string;
  category?: string;
  description?: string;
}

const Page = () => {
  const [collections, setCollections] = useState<Collection[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchCollections = async () => {
      try {
        const res = await fetch('/api/v1/collections');
        if (!res.ok) throw new Error('Failed to fetch');
        const data = await res.json();
        setCollections(data.items ?? []);
      } catch {
        toast.error('Failed to load collections');
      } finally {
        setLoading(false);
      }
    };
    fetchCollections();
  }, []);

  return (
    <div className="flex flex-col pt-10 pb-28 lg:pb-8 px-4 max-w-4xl mx-auto w-full">
      <div className="flex items-center gap-3 border-b border-light-200/20 dark:border-dark-200/20 pb-6">
        <FolderOpen size={40} className="text-[#7C3AED]" />
        <h1 className="text-4xl font-normal">Spaces</h1>
      </div>

      <div className="mt-4 flex gap-4">
        <Link
          href="/spaces/templates"
          className="text-sm text-[#7C3AED] hover:underline"
        >
          Space Templates →
        </Link>
      </div>

      <p className="mt-2 text-black/60 dark:text-white/60">
        Popular collections for research and analysis.
      </p>

      {loading ? (
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-6">
          {[1, 2, 3].map((i) => (
            <div
              key={i}
              className="rounded-2xl bg-light-secondary dark:bg-dark-secondary p-4 animate-pulse h-24"
            />
          ))}
        </div>
      ) : collections.length > 0 ? (
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-6">
          {collections.map((c) => (
            <Link
              key={c.id}
              href={`/collections/${c.id}`}
              className="rounded-2xl bg-light-secondary dark:bg-dark-secondary p-5 shadow-sm border border-light-200/20 dark:border-dark-200/20 block hover:border-[#7C3AED]/30 transition-colors"
            >
              <h2 className="font-medium text-lg">{c.title}</h2>
              {c.description && (
                <p className="text-sm text-black/60 dark:text-white/60 mt-1">{c.description}</p>
              )}
              {c.category && (
                <span className="inline-block mt-2 text-xs px-2 py-0.5 rounded bg-light-200 dark:bg-dark-200">
                  {c.category}
                </span>
              )}
            </Link>
          ))}
        </div>
      ) : (
        <div className="mt-6 p-6 rounded-2xl bg-light-secondary dark:bg-dark-secondary text-center text-black/60 dark:text-white/60">
          <p>No collections available. Enable projects-svc in deploy.config.yaml</p>
        </div>
      )}
    </div>
  );
};

export default Page;
