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

// Fetch fresh stats on each request
export const dynamic = "force-dynamic";

export default async function HomePage() {
  let stats;
  try {
    stats = await getStats();
  } catch {
    stats = null;
  }

  return (
    <div className="space-y-12">
      <section className="text-center py-12">
        <h1 className="text-4xl font-bold mb-4">Master Coding Interviews</h1>
        <p className="text-[var(--muted-foreground)] text-lg max-w-2xl mx-auto mb-8">
          Curated collection of coding interview questions with detailed
          explanations, common pitfalls, and optimal solutions.
        </p>
        <Link
          href="/questions"
          className="inline-block px-6 py-3 bg-[var(--primary)] text-[var(--primary-foreground)] rounded-lg font-medium hover:opacity-90 transition-opacity"
        >
          Browse Questions
        </Link>
      </section>

      {stats && (
        <section className="grid grid-cols-1 md:grid-cols-3 gap-6">
          <div className="p-6 rounded-lg border border-[var(--border)] bg-[var(--card)]">
            <div className="text-3xl font-bold text-[var(--primary)]">
              {stats.total_questions}
            </div>
            <div className="text-[var(--muted-foreground)]">
              Total Questions
            </div>
          </div>
          <div className="p-6 rounded-lg border border-[var(--border)] bg-[var(--card)]">
            <div className="text-3xl font-bold text-[var(--primary)]">
              {stats.by_category.length}
            </div>
            <div className="text-[var(--muted-foreground)]">Categories</div>
          </div>
          <div className="p-6 rounded-lg border border-[var(--border)] bg-[var(--card)]">
            <div className="text-3xl font-bold text-[var(--primary)]">
              {stats.by_pattern.length}
            </div>
            <div className="text-[var(--muted-foreground)]">
              Algorithmic Patterns
            </div>
          </div>
        </section>
      )}

      {stats && (
        <section className="grid grid-cols-1 md:grid-cols-3 gap-6">
          <div className="p-4 rounded-lg border border-green-200 bg-green-50 dark:border-green-900 dark:bg-green-900/20">
            <div className="text-2xl font-bold text-green-600 dark:text-green-400">
              {stats.by_difficulty.easy}
            </div>
            <div className="text-green-700 dark:text-green-300">Easy</div>
          </div>
          <div className="p-4 rounded-lg border border-yellow-200 bg-yellow-50 dark:border-yellow-900 dark:bg-yellow-900/20">
            <div className="text-2xl font-bold text-yellow-600 dark:text-yellow-400">
              {stats.by_difficulty.medium}
            </div>
            <div className="text-yellow-700 dark:text-yellow-300">Medium</div>
          </div>
          <div className="p-4 rounded-lg border border-red-200 bg-red-50 dark:border-red-900 dark:bg-red-900/20">
            <div className="text-2xl font-bold text-red-600 dark:text-red-400">
              {stats.by_difficulty.hard}
            </div>
            <div className="text-red-700 dark:text-red-300">Hard</div>
          </div>
        </section>
      )}

      <section className="space-y-4">
        <h2 className="text-2xl font-bold">Quick Links</h2>
        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          <Link
            href="/categories"
            className="p-6 rounded-lg border border-[var(--border)] bg-[var(--card)] hover:border-[var(--primary)] transition-colors"
          >
            <h3 className="font-semibold mb-2">Browse by Category</h3>
            <p className="text-[var(--muted-foreground)] text-sm">
              Arrays, Trees, Graphs, Dynamic Programming, and more
            </p>
          </Link>
          <Link
            href="/patterns"
            className="p-6 rounded-lg border border-[var(--border)] bg-[var(--card)] hover:border-[var(--primary)] transition-colors"
          >
            <h3 className="font-semibold mb-2">Browse by Pattern</h3>
            <p className="text-[var(--muted-foreground)] text-sm">
              Two Pointers, Sliding Window, BFS, DFS, Backtracking, and more
            </p>
          </Link>
        </div>
      </section>
    </div>
  );
}
