'use client'

import { useParams, useRouter } from 'next/navigation'
import Link from 'next/link'
import type { Route } from 'next'
import { trpc } from '@/lib/trpc/client'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Skeleton } from '@/components/ui/skeleton'
import { ArrowLeft, CheckCircle2, Clock, Circle, Eye } from 'lucide-react'
import { toast } from 'sonner'
import { JurorProgressDashboard } from '@/components/jury/juror-progress-dashboard'

export default function JuryRoundDetailPage() {
  const params = useParams()
  const router = useRouter()
  const roundId = params.roundId as string

  const { data: assignments, isLoading } = trpc.roundAssignment.getMyAssignments.useQuery(
    { roundId },
    { enabled: !!roundId, refetchInterval: 30_000 }
  )

  const { data: round } = trpc.round.getById.useQuery(
    { id: roundId },
    { enabled: !!roundId, refetchInterval: 30_000 }
  )

  if (isLoading) {
    return (
      <div className="space-y-6">
        <Skeleton className="h-8 w-64" />
        <Skeleton className="h-64" />
      </div>
    )
  }

  return (
    <div className="space-y-6">
      <div className="flex items-center gap-4">
        <Button variant="ghost" size="sm" onClick={() => router.back()}>
          <ArrowLeft className="mr-2 h-4 w-4" />
          Back
        </Button>
        <div>
          <h1 className="text-2xl font-bold tracking-tight text-brand-blue">
            {round?.name || 'Round Details'}
          </h1>
          <p className="text-muted-foreground mt-1">
            Your assigned projects for this round
          </p>
        </div>
      </div>

      {(() => {
        const config = (round?.configJson as Record<string, unknown>) ?? {}
        if (config.showJurorProgressDashboard) {
          return <JurorProgressDashboard roundId={roundId} />
        }
        return null
      })()}

      <Card>
        <CardHeader>
          <CardTitle>Assigned Projects</CardTitle>
        </CardHeader>
        <CardContent>
          {!assignments || assignments.length === 0 ? (
            <div className="flex flex-col items-center justify-center py-8 text-center">
              <Circle className="h-12 w-12 text-muted-foreground/50 mb-3" />
              <p className="font-medium">No assignments yet</p>
              <p className="text-sm text-muted-foreground mt-1">
                You will see your assigned projects here once they are assigned
              </p>
            </div>
          ) : (
            <div className="space-y-2">
              {assignments.map((assignment) => {
                const isCompleted = assignment.evaluation?.status === 'SUBMITTED'
                const isDraft = assignment.evaluation?.status === 'DRAFT'

                return (
                  <div
                    key={assignment.id}
                    role="button"
                    tabIndex={0}
                    onClick={() => router.push(`/jury/competitions/${roundId}/projects/${assignment.projectId}`)}
                    onKeyDown={(e) => { if (e.key === 'Enter') router.push(`/jury/competitions/${roundId}/projects/${assignment.projectId}`) }}
                    className="flex items-center justify-between p-4 rounded-lg border border-border/60 hover:border-brand-blue/30 hover:bg-brand-blue/5 transition-all cursor-pointer"
                  >
                    <div className="flex-1 min-w-0">
                      <p className="font-medium truncate">{assignment.project.title}</p>
                      <div className="flex items-center gap-2 mt-1 flex-wrap">
                        {assignment.project.competitionCategory && (
                          <Badge variant="secondary" className="text-xs">
                            {assignment.project.competitionCategory}
                          </Badge>
                        )}
                      </div>
                    </div>
                    <div className="flex items-center gap-2 ml-4">
                      {isCompleted ? (
                        <>
                          <Badge variant="default" className="bg-emerald-50 text-emerald-700 border-emerald-200">
                            <CheckCircle2 className="mr-1 h-3 w-3" />
                            Completed
                          </Badge>
                          <Button
                            variant="outline"
                            size="sm"
                            className="h-7 text-xs"
                            asChild
                            onClick={(e) => e.stopPropagation()}
                          >
                            <Link href={`/jury/competitions/${roundId}/projects/${assignment.projectId}/evaluate` as Route}>
                              <Eye className="mr-1 h-3 w-3" />
                              View
                            </Link>
                          </Button>
                        </>
                      ) : isDraft ? (
                        <Badge variant="secondary" className="bg-amber-50 text-amber-700 border-amber-200">
                          <Clock className="mr-1 h-3 w-3" />
                          Draft
                        </Badge>
                      ) : (
                        <Badge variant="outline">
                          <Circle className="mr-1 h-3 w-3" />
                          Pending
                        </Badge>
                      )}
                    </div>
                  </div>
                )
              })}
            </div>
          )}
        </CardContent>
      </Card>
    </div>
  )
}
