"use client";

import { Badge } from "~/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
import { api } from "~/trpc/react";
import { formatBytes } from "~/lib/utils";

export function SystemStats() {
  const { data: stats, isLoading } = api.admin.getSystemStats.useQuery({});

  if (isLoading || !stats) {
    return (
      <div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-4">
        {Array.from({ length: 4 }).map((_, i) => (
          <Card key={i} className="animate-pulse">
            <CardHeader className="pb-2">
              <div className="h-4 w-20 rounded bg-slate-200"></div>
            </CardHeader>
            <CardContent>
              <div className="mb-2 h-8 w-12 rounded bg-slate-200"></div>
              <div className="h-3 w-24 rounded bg-slate-200"></div>
            </CardContent>
          </Card>
        ))}
      </div>
    );
  }

  const formatUptime = (seconds: number) => {
    const d = Math.floor(seconds / (3600 * 24));
    const h = Math.floor((seconds % (3600 * 24)) / 3600);
    return `${d} days, ${h} hours`;
  };

  const displayStats = {
    totalUsers: stats.users.total,
    totalStudies: stats.studies.total,
    totalExperiments: stats.experiments.total,
    totalTrials: stats.trials.total,
    activeTrials: stats.trials.running,
    systemHealth: "healthy",
    uptime: formatUptime(stats.system.uptime),
    storageUsed: formatBytes(stats.storage.totalSize),
  };

  return (
    <div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-4">
      {/* Total Users */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Total Users
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{displayStats.totalUsers}</div>
          <div className="mt-1 flex items-center gap-2">
            <Badge variant="secondary" className="text-xs">
              All roles
            </Badge>
          </div>
        </CardContent>
      </Card>

      {/* Total Studies */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Studies
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{displayStats.totalStudies}</div>
          <div className="mt-1 flex items-center gap-2">
            <Badge variant="secondary" className="text-xs">
              Active
            </Badge>
          </div>
        </CardContent>
      </Card>

      {/* Total Experiments */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Experiments
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">
            {displayStats.totalExperiments}
          </div>
          <div className="mt-1 flex items-center gap-2">
            <Badge variant="secondary" className="text-xs">
              Published
            </Badge>
          </div>
        </CardContent>
      </Card>

      {/* Total Trials */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Trials
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{displayStats.totalTrials}</div>
          <div className="mt-1 flex items-center gap-2">
            <Badge variant="outline" className="text-xs">
              {displayStats.activeTrials} running
            </Badge>
          </div>
        </CardContent>
      </Card>

      {/* System Health */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            System Health
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="flex items-center gap-2">
            <div className="flex h-3 w-3 items-center justify-center">
              <div className="h-2 w-2 rounded-full bg-green-500"></div>
            </div>
            <span className="text-sm font-medium text-green-600">
              {displayStats.systemHealth === "healthy" ? "Healthy" : "Issues"}
            </span>
          </div>
          <div className="text-muted-foreground mt-1 text-xs">
            All services operational
          </div>
        </CardContent>
      </Card>

      {/* Uptime */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Uptime
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="text-sm font-medium">{displayStats.uptime}</div>
          <div className="text-muted-foreground mt-1 text-xs">
            Since last restart
          </div>
        </CardContent>
      </Card>

      {/* Storage Usage */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Storage Used
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="text-sm font-medium">{displayStats.storageUsed}</div>
          <div className="text-muted-foreground mt-1 text-xs">
            Media & database
          </div>
        </CardContent>
      </Card>

      {/* Recent Activity */}
      <Card>
        <CardHeader className="pb-2">
          <CardTitle className="text-muted-foreground text-sm font-medium">
            Recent Activity
          </CardTitle>
        </CardHeader>
        <CardContent>
          <div className="space-y-1">
            <div className="text-muted-foreground text-xs">
              2 trials started today
            </div>
            <div className="text-muted-foreground text-xs">
              1 new user registered
            </div>
            <div className="text-muted-foreground text-xs">
              3 experiments published
            </div>
          </div>
        </CardContent>
      </Card>
    </div>
  );
}
