import { getTranslations } from "next-intl/server"

import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card"
import { Badge } from "@/shared/components/ui/badge"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/shared/components/ui/table"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { Trophy } from "lucide-react"

import { GradeStatsCard } from "./grade-stats-card"
import type { ClassGradeStats, ClassRankingItem } from "../types"

interface ClassGradeReportProps {
  stats: ClassGradeStats | null
  ranking: ClassRankingItem[]
}

export async function ClassGradeReport({ stats, ranking }: ClassGradeReportProps) {
  const t = await getTranslations("grades")
  return (
    <div className="space-y-6">
      {stats ? (
        <div className="space-y-4">
          <div className="flex items-center justify-between">
            <div>
              <h3 className="text-lg font-semibold">{stats.className}</h3>
              <p className="text-sm text-muted-foreground">
                {t("classReport.studentCountInfo", { studentCount: stats.studentCount, recordCount: stats.stats.count })}
              </p>
            </div>
          </div>
          <GradeStatsCard stats={stats.stats} />
        </div>
      ) : (
        <EmptyState
          title={t("classReport.noDataTitle")}
          description={t("classReport.noDataDescription")}
          icon={Trophy}
          className="border-none shadow-none"
        />
      )}

      {ranking.length > 0 ? (
        <Card>
          <CardHeader>
            <CardTitle>{t("classReport.classRanking")}</CardTitle>
          </CardHeader>
          <CardContent>
            <div className="rounded-md border">
              <Table>
                <caption className="sr-only">{t("classReport.caption")}</caption>
                <TableHeader>
                  <TableRow>
                    <TableHead className="w-16">{t("classReport.rankColumn")}</TableHead>
                    <TableHead>{t("list.columns.student")}</TableHead>
                    <TableHead className="text-right">{t("summary.averageScore")}</TableHead>
                    <TableHead className="text-right">{t("classReport.recordsColumn")}</TableHead>
                  </TableRow>
                </TableHeader>
                <TableBody>
                  {ranking.map((r) => (
                    <TableRow key={r.studentId}>
                      <TableCell>
                        <Badge
                          variant={
                            r.rank === 1 ? "default" : r.rank <= 3 ? "secondary" : "outline"
                          }
                          className="font-mono"
                        >
                          #{r.rank}
                        </Badge>
                      </TableCell>
                      <TableCell className="font-medium">{r.studentName}</TableCell>
                      <TableCell className="text-right font-mono">
                        {r.averageScore.toFixed(2)}
                      </TableCell>
                      <TableCell className="text-right text-muted-foreground">
                        {r.recordCount}
                      </TableCell>
                    </TableRow>
                  ))}
                </TableBody>
              </Table>
            </div>
          </CardContent>
        </Card>
      ) : null}
    </div>
  )
}
