"use client";

import { useEffect, useState } from "react";
import Link from "next/link";
import { supabase } from "@/lib/supabase";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Dumbbell, Users, FolderOpen, Flame } from "lucide-react";

interface Stats {
  workouts: number;
  trainers: number;
  collections: number;
}

export default function DashboardPage() {
  const [stats, setStats] = useState<Stats>({ workouts: 0, trainers: 0, collections: 0 });
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetchStats();
  }, []);

  const fetchStats = async () => {
    try {
      const [{ count: workouts }, { count: trainers }, { count: collections }] = await Promise.all([
        supabase.from("workouts").select("*", { count: "exact", head: true }),
        supabase.from("trainers").select("*", { count: "exact", head: true }),
        supabase.from("collections").select("*", { count: "exact", head: true }),
      ]);

      setStats({
        workouts: workouts || 0,
        trainers: trainers || 0,
        collections: collections || 0,
      });
    } catch (error) {
      console.error("Failed to fetch stats:", error);
    } finally {
      setLoading(false);
    }
  };

  const statCards = [
    { title: "Workouts", value: stats.workouts, icon: Dumbbell, href: "/workouts", color: "text-orange-500" },
    { title: "Trainers", value: stats.trainers, icon: Users, href: "/trainers", color: "text-blue-500" },
    { title: "Collections", value: stats.collections, icon: FolderOpen, href: "/collections", color: "text-green-500" },
  ];

  return (
    <div className="p-8">
      <div className="mb-8">
        <h1 className="text-3xl font-bold text-white mb-2">Dashboard</h1>
        <p className="text-neutral-400">Overview of your TabataFit content</p>
      </div>

      <div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
        {statCards.map((stat) => {
          const Icon = stat.icon;
          return (
            <Link key={stat.title} href={stat.href}>
              <Card className="bg-neutral-900 border-neutral-800 hover:border-neutral-700 transition-colors cursor-pointer">
                <CardHeader className="flex flex-row items-center justify-between pb-2">
                  <CardTitle className="text-sm font-medium text-neutral-400">
                    {stat.title}
                  </CardTitle>
                  <Icon className={`w-5 h-5 ${stat.color}`} />
                </CardHeader>
                <CardContent>
                  <div className="text-3xl font-bold text-white">
                    {loading ? "-" : stat.value}
                  </div>
                </CardContent>
              </Card>
            </Link>
          );
        })}
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <Card className="bg-neutral-900 border-neutral-800">
          <CardHeader>
            <CardTitle className="text-white flex items-center gap-2">
              <Flame className="w-5 h-5 text-orange-500" />
              Quick Actions
            </CardTitle>
          </CardHeader>
          <CardContent className="space-y-3">
            <Link
              href="/workouts"
              className="block p-4 bg-neutral-950 rounded-lg hover:bg-neutral-800 transition-colors"
            >
              <div className="font-medium text-white">Manage Workouts</div>
              <div className="text-sm text-neutral-400">Add, edit, or remove workouts</div>
            </Link>
            <Link
              href="/trainers"
              className="block p-4 bg-neutral-950 rounded-lg hover:bg-neutral-800 transition-colors"
            >
              <div className="font-medium text-white">Manage Trainers</div>
              <div className="text-sm text-neutral-400">Update trainer profiles and photos</div>
            </Link>
            <Link
              href="/media"
              className="block p-4 bg-neutral-950 rounded-lg hover:bg-neutral-800 transition-colors"
            >
              <div className="font-medium text-white">Upload Media</div>
              <div className="text-sm text-neutral-400">Add videos and thumbnails</div>
            </Link>
          </CardContent>
        </Card>

        <Card className="bg-neutral-900 border-neutral-800">
          <CardHeader>
            <CardTitle className="text-white">Getting Started</CardTitle>
          </CardHeader>
          <CardContent className="space-y-4 text-neutral-400">
            <p>
              Welcome to the TabataFit Admin Dashboard. Here you can manage all your
              fitness content.
            </p>
            <ul className="list-disc list-inside space-y-2 text-sm">
              <li>Create and edit Tabata workouts</li>
              <li>Manage trainer profiles</li>
              <li>Organize workouts into collections</li>
              <li>Upload workout videos and thumbnails</li>
            </ul>
          </CardContent>
        </Card>
      </div>
    </div>
  );
}
