"use client";

import { useEffect, useState } from "react";
import { Card } from "@/components/ui/card";
import { Header } from "@/components/header";
import { ControllerTable } from "@/components/controller-table";
import { prisma } from '@/lib/prisma'

export default function ControllersPage() {
  const [controllers, setControllers] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    async function loadControllers() {
      try {
        const response = await fetch('/api/controllers');
        if (!response.ok) throw new Error('Failed to fetch METAR data');
        const uniqueControllers = await response.json();
        
        setControllers(uniqueControllers);
      } catch (error) {
        console.error("Error fetching controller data:", error);
      } finally {
        setLoading(false);
      }
    }

    loadControllers();
  }, []);

  return (
    <main className="min-h-screen bg-gradient-to-b from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800">
      <div className="container mx-auto px-4 py-8">
        <div className="flex flex-col gap-8">
          <Header 
            title="Controller History" 
            subtitle="View recent controller activity" 
          />

          <Card className="overflow-hidden">
            <ControllerTable data={controllers} loading={loading} />
          </Card>
        </div>
      </div>
    </main>
  );
}