import { Suspense } from 'react';

import { ChartSkeleton } from '@/components/charts/chart-skeleton.js';
import { MetricCardSkeleton } from '@/components/dashboard/metric-card-skeleton.js';
import { Card, CardContent, CardHeader } from '@/components/ui/card.js';
import { Skeleton } from '@/components/ui/skeleton.js';
import { Activity, ArrowRight, Map as MapIcon } from 'lucide-react';
import Link from 'next/link';

import { AlertsSection } from './_sections/alerts-section.js';
import { DemandSummary } from './_sections/demand-summary.js';
import { GpuCalculatorSection } from './_sections/gpu-calculator-section.js';
import { HeroMetrics } from './_sections/hero-metrics.js';
import { PricesByRegion } from './_sections/prices-by-region.js';
import { StressGauges } from './_sections/stress-gauges.js';

function MetricCardsSkeleton() {
  return (
    <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-5">
      {Array.from({ length: 5 }).map((_, i) => (
        <MetricCardSkeleton key={i} />
      ))}
    </div>
  );
}

function PricesByRegionSkeleton() {
  return (
    <Card>
      <CardHeader>
        <Skeleton className="h-5 w-48" />
      </CardHeader>
      <CardContent>
        <div className="space-y-3">
          {Array.from({ length: 5 }).map((_, i) => (
            <div key={i} className="flex items-center justify-between">
              <Skeleton className="h-4 w-24" />
              <Skeleton className="h-4 w-20" />
            </div>
          ))}
        </div>
      </CardContent>
    </Card>
  );
}

function AlertsSkeleton() {
  return (
    <Card>
      <CardHeader>
        <Skeleton className="h-5 w-32" />
      </CardHeader>
      <CardContent>
        <div className="space-y-3">
          {Array.from({ length: 3 }).map((_, i) => (
            <div key={i} className="flex items-center gap-3">
              <Skeleton className="h-8 w-8 rounded-full" />
              <div className="flex-1 space-y-1.5">
                <Skeleton className="h-4 w-48" />
                <Skeleton className="h-3 w-32" />
              </div>
            </div>
          ))}
        </div>
      </CardContent>
    </Card>
  );
}

function GaugesSkeleton() {
  return (
    <Card>
      <CardHeader className="pb-3">
        <Skeleton className="h-5 w-44" />
        <Skeleton className="mt-1 h-3 w-64" />
      </CardHeader>
      <CardContent>
        <div className="grid gap-x-8 gap-y-2.5 md:grid-cols-2">
          {Array.from({ length: 2 }).map((_, col) => (
            <div key={col} className="flex flex-col gap-2.5">
              {Array.from({ length: 5 }).map((_, i) => (
                <div key={i} className="flex items-center gap-3">
                  <Skeleton className="h-4 w-14" />
                  <Skeleton className="h-2 flex-1 rounded-full" />
                  <Skeleton className="h-4 w-20" />
                </div>
              ))}
            </div>
          ))}
        </div>
      </CardContent>
    </Card>
  );
}

function DemandSummarySkeleton() {
  return (
    <Card>
      <CardHeader className="pb-3">
        <div className="flex items-center gap-2">
          <Activity className="h-5 w-5 text-chart-3" />
          <Skeleton className="h-5 w-40" />
        </div>
        <Skeleton className="mt-1 h-3 w-56" />
      </CardHeader>
      <CardContent>
        <div className="grid grid-cols-2 gap-4">
          {Array.from({ length: 4 }).map((_, i) => (
            <div key={i} className="flex flex-col gap-1">
              <Skeleton className="h-3 w-20" />
              <Skeleton className="h-6 w-16" />
            </div>
          ))}
        </div>
      </CardContent>
    </Card>
  );
}

export default function DashboardHome() {
  return (
    <div className="px-4 py-6 sm:px-6 sm:py-8">
      <div className="mb-6 flex items-end justify-between">
        <div>
          <h1 className="text-2xl font-bold tracking-tight sm:text-3xl">Dashboard</h1>
          <p className="mt-1 text-sm text-muted-foreground">
            Real-time overview of AI datacenter energy impact across US grid regions.
          </p>
        </div>
        <Link
          href="/map"
          className="group hidden items-center gap-1.5 rounded-md border border-border/50 bg-muted/30 px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:border-primary/50 hover:text-foreground sm:inline-flex">
          <MapIcon className="h-3.5 w-3.5" />
          Open Map
          <ArrowRight className="h-3 w-3 transition-transform group-hover:translate-x-0.5" />
        </Link>
      </div>

      {/* Hero metric cards */}
      <Suspense fallback={<MetricCardsSkeleton />}>
        <HeroMetrics />
      </Suspense>

      {/* Row 2: Prices + Demand Highlights + Alerts — three-column on large screens */}
      <div className="mt-6 grid gap-4 lg:grid-cols-3">
        <Suspense fallback={<PricesByRegionSkeleton />}>
          <PricesByRegion />
        </Suspense>

        <Suspense fallback={<DemandSummarySkeleton />}>
          <DemandSummary />
        </Suspense>

        <Suspense fallback={<AlertsSkeleton />}>
          <AlertsSection />
        </Suspense>
      </div>

      {/* Row 3: GPU Calculator + Grid Stress side by side */}
      <div className="mt-6 grid gap-4 lg:grid-cols-[1fr_1fr]">
        <Suspense fallback={<ChartSkeleton />}>
          <GpuCalculatorSection />
        </Suspense>

        <Suspense fallback={<GaugesSkeleton />}>
          <StressGauges />
        </Suspense>
      </div>
    </div>
  );
}
