'use client'

import Link from 'next/link'
import { trpc } from '@/lib/trpc/client'
import { Button } from '@/components/ui/button'
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { Skeleton } from '@/components/ui/skeleton'
import { Plus, Trophy, Users, CheckCircle2 } from 'lucide-react'

const STATUS_COLORS: Record<string, 'default' | 'secondary' | 'destructive' | 'outline'> = {
  DRAFT: 'secondary',
  NOMINATIONS_OPEN: 'default',
  VOTING_OPEN: 'default',
  CLOSED: 'outline',
  ARCHIVED: 'secondary',
}

const SCORING_LABELS: Record<string, string> = {
  PICK_WINNER: 'Pick Winner',
  RANKED: 'Ranked',
  SCORED: 'Scored',
}

export default function AwardsListPage() {
  const { data: awards, isLoading } = trpc.specialAward.list.useQuery({})

  if (isLoading) {
    return (
      <div className="space-y-6">
        <div className="flex items-center justify-between">
          <Skeleton className="h-9 w-48" />
          <Skeleton className="h-9 w-32" />
        </div>
        <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
          {[...Array(3)].map((_, i) => (
            <Skeleton key={i} className="h-48" />
          ))}
        </div>
      </div>
    )
  }

  return (
    <div className="space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-semibold tracking-tight">
            Special Awards
          </h1>
          <p className="text-muted-foreground">
            Manage named awards with eligibility criteria and jury voting
          </p>
        </div>
        <Button asChild>
          <Link href="/admin/awards/new">
            <Plus className="mr-2 h-4 w-4" />
            Create Award
          </Link>
        </Button>
      </div>

      {/* Awards Grid */}
      {awards && awards.length > 0 ? (
        <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
          {awards.map((award) => (
            <Link key={award.id} href={`/admin/awards/${award.id}`}>
              <Card className="transition-colors hover:bg-muted/50 cursor-pointer h-full">
                <CardHeader className="pb-3">
                  <div className="flex items-start justify-between">
                    <CardTitle className="text-lg flex items-center gap-2">
                      <Trophy className="h-5 w-5 text-amber-500" />
                      {award.name}
                    </CardTitle>
                    <Badge variant={STATUS_COLORS[award.status] || 'secondary'}>
                      {award.status.replace('_', ' ')}
                    </Badge>
                  </div>
                  {award.description && (
                    <CardDescription className="line-clamp-2">
                      {award.description}
                    </CardDescription>
                  )}
                </CardHeader>
                <CardContent>
                  <div className="flex items-center gap-4 text-sm text-muted-foreground">
                    <div className="flex items-center gap-1">
                      <CheckCircle2 className="h-4 w-4" />
                      {award._count.eligibilities} eligible
                    </div>
                    <div className="flex items-center gap-1">
                      <Users className="h-4 w-4" />
                      {award._count.jurors} jurors
                    </div>
                    <Badge variant="outline" className="text-xs">
                      {SCORING_LABELS[award.scoringMode] || award.scoringMode}
                    </Badge>
                  </div>
                  {award.winnerProject && (
                    <div className="mt-3 pt-3 border-t">
                      <p className="text-sm">
                        <span className="text-muted-foreground">Winner:</span>{' '}
                        <span className="font-medium">
                          {award.winnerProject.title}
                        </span>
                      </p>
                    </div>
                  )}
                </CardContent>
              </Card>
            </Link>
          ))}
        </div>
      ) : (
        <Card>
          <CardContent className="flex flex-col items-center justify-center py-12 text-center">
            <Trophy className="h-12 w-12 text-muted-foreground/50" />
            <p className="mt-2 font-medium">No awards yet</p>
            <p className="text-sm text-muted-foreground">
              Create special awards for outstanding projects
            </p>
            <Button className="mt-4" asChild>
              <Link href="/admin/awards/new">
                <Plus className="mr-2 h-4 w-4" />
                Create Award
              </Link>
            </Button>
          </CardContent>
        </Card>
      )}
    </div>
  )
}
