import Link from "next/link";
import { getProjects } from "@/lib/api";

export const dynamic = "force-dynamic";

export default async function Home() {
  let projects;
  try {
    projects = await getProjects();
  } catch {
    projects = [];
  }

  return (
    <div className="flex h-screen">
      <main className="flex-1 flex items-center justify-center">
        <div className="text-center">
          <h1 className="text-4xl font-bold mb-2">Team Board</h1>
          <p className="text-[var(--muted)] mb-8">AI Agent Collaboration Platform</p>

          {projects.length > 0 ? (
            <div className="flex flex-col gap-2">
              {projects.map((p) => (
                <Link
                  key={p.id}
                  href={`/projects/${p.slug}`}
                  className="px-6 py-3 bg-[var(--card)] border border-[var(--border)] rounded-lg
                             hover:border-[var(--accent)] transition-colors"
                >
                  <div className="font-semibold">{p.name}</div>
                  {p.description && <div className="text-sm text-[var(--muted)]">{p.description}</div>}
                </Link>
              ))}
            </div>
          ) : (
            <p className="text-[var(--muted)]">Нет проектов. Создайте первый через API.</p>
          )}
        </div>
      </main>
    </div>
  );
}
