'use client'

import { motion } from 'motion/react'

type RoundStatsSummaryProps = {
  projectCount: number
  newProjectsThisWeek: number
  totalJurors: number
  activeJurors: number
  totalAssignments: number
  evaluationStats: Array<{ status: string; _count: number }>
  actionsCount: number
  nextDraftRound?: { name: string; roundType: string } | null
}

export function RoundStatsSummary({
  projectCount,
  totalJurors,
  activeJurors,
  totalAssignments,
  evaluationStats,
  actionsCount,
  nextDraftRound,
}: RoundStatsSummaryProps) {
  const submittedCount =
    evaluationStats.find((e) => e.status === 'SUBMITTED')?._count ?? 0
  const completionPct =
    totalAssignments > 0 ? ((submittedCount / totalAssignments) * 100).toFixed(0) : '—'

  const stats = [
    {
      value: projectCount,
      label: 'Total projects',
      detail: 'In this edition',
      accent: 'text-brand-blue',
    },
    {
      value: `${activeJurors}/${totalJurors}`,
      label: 'Jury coverage',
      detail: totalJurors > 0 ? `${activeJurors} active jurors` : 'No jurors assigned',
      accent: 'text-brand-teal',
    },
    {
      value: totalAssignments > 0 ? `${completionPct}%` : '—',
      label: 'Completion',
      detail: totalAssignments > 0 ? `${submittedCount}/${totalAssignments} evaluations` : 'No evaluations yet',
      accent: 'text-emerald-600',
    },
    {
      value: actionsCount,
      label: actionsCount === 1 ? 'Action' : 'Actions',
      detail: nextDraftRound
        ? `Next: ${nextDraftRound.name}`
        : actionsCount > 0 ? 'Pending' : 'All clear',
      accent: actionsCount > 0 ? 'text-amber-600' : 'text-emerald-600',
    },
  ]

  return (
    <>
      <p className="mb-2 text-[11px] font-semibold uppercase tracking-widest text-muted-foreground/70">
        No active round &mdash; Competition Summary
      </p>

      <motion.div
        initial={{ opacity: 0 }}
        animate={{ opacity: 1 }}
        transition={{ duration: 0.4 }}
        className="flex items-baseline justify-between border-b border-t py-3 md:hidden"
      >
        {stats.map((s, i) => (
          <div key={i} className={`flex-1 text-center ${i > 0 ? 'border-l border-border/50' : ''}`}>
            <span className="text-xl font-bold tabular-nums tracking-tight">{s.value}</span>
            <p className="text-[10px] font-medium uppercase tracking-wider text-muted-foreground mt-0.5">{s.label}</p>
          </div>
        ))}
      </motion.div>

      <div className="hidden md:block">
        <div className="grid grid-cols-4 gap-px rounded-lg bg-border/40 overflow-hidden">
          {stats.map((s, i) => (
            <motion.div
              key={i}
              initial={{ opacity: 0, y: 8 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ duration: 0.3, delay: i * 0.06 }}
              className="bg-background px-5 py-4 group hover:bg-muted/30 transition-colors"
            >
              <span className="text-3xl font-bold tabular-nums tracking-tight">{s.value}</span>
              <p className="text-xs font-medium text-muted-foreground mt-1">{s.label}</p>
              <p className={`text-xs mt-0.5 ${s.accent}`}>{s.detail}</p>
            </motion.div>
          ))}
        </div>
      </div>
    </>
  )
}
