// app/dashboard/page.tsx
// Dashboard - navbar/footer now in layout.tsx

import { query } from '@/lib/db';
import { DriverWithServer } from '@/types/racing';
import { UsersIcon, ServerIcon, ActivityIcon, MapPinIcon, FlagIcon, LiveDotIcon } from '@/components/ui/icons';
import { cleanTrackName, cleanTrackConfig } from '@/lib/trackUtils';
export const dynamic = "force-dynamic";

async function getConnectedDrivers(): Promise<DriverWithServer[]> {
  const sql = `
    SELECT 
      u.driver_guid,
      u.driver_name,
      u.driver_team,
      u.car_model,
      u.car_skin,
      u.user_rank,
      u.laps_completed,
      s.server_id,
      s.server_name,
      s.server_track,
      s.session_flag,
      s.connected_players
    FROM users u
    INNER JOIN servers s ON u.current_server = s.server_id
    WHERE u.is_connect = true
    ORDER BY s.server_id, u.user_rank ASC
  `;

  const rows = await query(sql);

  return rows.map((row: any) => ({
    driver_guid: row.driver_guid,
    driver_name: row.driver_name,
    driver_team: row.driver_team,
    car_model: row.car_model,
    car_skin: row.car_skin,
    cuts_alltime: 0,
    contacts_alltime: 0,
    laps_completed: row.laps_completed,
    user_rank: row.user_rank,
    is_connect: true,
    is_loading: false,
    current_server: row.server_id,
    created_at: new Date(),
    server: {
      server_id: row.server_id,
      server_name: row.server_name,
      server_track: row.server_track,
      session_type: 0,
      session_flag: row.session_flag,
      connected_players: row.connected_players,
    },
  }));
}

export default async function DashboardPage() {
  const drivers = await getConnectedDrivers();

  const serverGroups = drivers.reduce((acc, driver) => {
    const serverId = driver.server?.server_id ?? 0;
    if (!acc[serverId]) {
      acc[serverId] = [];
    }
    acc[serverId].push(driver);
    return acc;
  }, {} as Record<number, DriverWithServer[]>);

  return (
    <>
      {/* Hero */}
      <div className="relative border-b border-white/10 grid-overlay">
        <div className="max-w-7xl mx-auto px-6 py-16">
          <div className="space-y-4">
            <div className="inline-flex items-center space-x-2 px-3 py-1 border border-white/20">
              <LiveDotIcon className="w-2 h-2 text-white animate-pulse" />
              <span className="text-xs font-medium tracking-wider">LIVE SYSTEM</span>
            </div>
            <h1 className="text-6xl font-bold tracking-tight">
              LIVE DASHBOARD
            </h1>
            <p className="text-white/60 text-lg max-w-2xl">
              Real-time telemetry and session monitoring across all OpenWheels Racing infrastructure
            </p>
          </div>

          {/* Stats Grid */}
          <div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-12">
            <StatCard 
              title="DRIVERS ONLINE" 
              value={drivers.length}
              icon={<UsersIcon className="w-8 h-8" />}
            />
            <StatCard 
              title="ACTIVE SERVERS" 
              value={Object.keys(serverGroups).length}
              icon={<ServerIcon className="w-8 h-8" />}
            />
            <StatCard 
              title="TOTAL LAPS" 
              value={drivers.reduce((sum, d) => sum + d.laps_completed, 0)}
              icon={<ActivityIcon className="w-8 h-8" />}
            />
          </div>
        </div>
      </div>

      {/* Server Listings */}
      <div className="max-w-7xl mx-auto px-6 py-12 space-y-6">
        {Object.keys(serverGroups).length === 0 ? (
          <div className="border border-white/10 p-16 text-center bg-black">
            <div className="w-16 h-16 border-2 border-white/20 mx-auto mb-6"></div>
            <p className="text-white/40 text-base tracking-wider">NO ACTIVE SESSIONS</p>
            <p className="text-white/20 text-sm mt-2">System idle — waiting for connections</p>
          </div>
        ) : (
          Object.entries(serverGroups).map(([serverId, serverDrivers]) => {
            const server = serverDrivers[0].server;
            return (
              <div key={serverId} className="border border-white/10 sharp-border bg-black">
                {/* Server Header */}
                <div className="border-b border-white/10 p-6 topo-lines-dense">
                  <div className="flex items-start justify-between">
                    <div className="flex-1 space-y-3">
                      <div className="flex items-center space-x-3">
                        <div className="flex items-center space-x-2 px-2 py-1 border border-white/30 text-xs">
                          <LiveDotIcon className="w-2 h-2 animate-pulse" />
                          <span className="font-medium tracking-wider">LIVE</span>
                        </div>
                        <span className="text-xs text-white/40 tracking-wider">
                          ID: {server?.server_id}
                        </span>
                      </div>
                      <h2 className="text-2xl font-light tracking-tight" style={{ letterSpacing: "0.1em" }}>
                        {server?.server_name}
                      </h2>
                      <div className="flex flex-wrap gap-x-6 gap-y-2">
                        <div className="flex items-center space-x-2">
                          <MapPinIcon className="w-4 h-4 text-white/60" />
                          <span className="text-white/80 text-sm">{server?.server_track}</span>
                        </div>
                        <div className="flex items-center space-x-2">
                          <FlagIcon className="w-4 h-4 text-white/60" />
                          <span className="text-white/80 text-sm">{server?.session_flag}</span>
                        </div>
                        <div className="flex items-center space-x-2">
                          <UsersIcon className="w-4 h-4 text-white/60" />
                          <span className="text-white/80 text-sm">{server?.connected_players} CONNECTED</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

                {/* Driver Table */}
                <div className="overflow-x-auto">
                  <table className="w-full">
                    <thead>
                      <tr className="border-b border-white/10">
                        <th className="px-6 py-4 text-left text-xs font-bold tracking-wider text-white/60">POS</th>
                        <th className="px-6 py-4 text-left text-xs font-bold tracking-wider text-white/60">DRIVER</th>
                        <th className="px-6 py-4 text-left text-xs font-bold tracking-wider text-white/60">TEAM</th>
                        <th className="px-6 py-4 text-left text-xs font-bold tracking-wider text-white/60">CAR</th>
                        <th className="px-6 py-4 text-left text-xs font-bold tracking-wider text-white/60">RANK</th>
                        <th className="px-6 py-4 text-left text-xs font-bold tracking-wider text-white/60">LAPS</th>
                      </tr>
                    </thead>
                    <tbody>
                      {serverDrivers.map((driver, index) => (
                        <tr 
                          key={driver.driver_guid} 
                          className="border-b border-white/5 hover:bg-white/5 transition-colors"
                        >
                          <td className="px-6 py-4">
                            <span className="text-base font-bold tracking-tight">
                              {String(index + 1).padStart(2, '0')}
                            </span>
                          </td>
                          <td className="px-6 py-4">
                            <span className="font-semibold tracking-tight text-base">{driver.driver_name}</span>
                          </td>
                          <td className="px-6 py-4">
                            <span className="text-white/50 text-sm">
                              {driver.driver_team || '—'}
                            </span>
                          </td>
                          <td className="px-6 py-4">
                            <span className="text-white/70 text-sm font-mono tracking-tight">
                              {driver.car_model}
                            </span>
                          </td>
                          <td className="px-6 py-4">
                            <span className="inline-block px-3 py-1 border border-white/20 text-sm font-mono">
                              {driver.user_rank}
                            </span>
                          </td>
                          <td className="px-6 py-4">
                            <span className="font-mono text-sm">{driver.laps_completed}</span>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </div>
            );
          })
        )}
      </div>
    </>
  );
}

function StatCard({ 
  title, 
  value, 
  icon 
}: { 
  title: string; 
  value: number; 
  icon: React.ReactNode;
}) {
  return (
    <div className="border border-white/10 p-6 sharp-border">
      <div className="flex items-center justify-between mb-4">
        <span className="text-xs font-bold tracking-wider text-white/60">{title}</span>
        <div className="text-white/40">
          {icon}
        </div>
      </div>
      <div className="text-5xl font-bold tracking-tight">
        {value.toLocaleString()}
      </div>
    </div>
  );
}
