'use client'

import { useSession } from 'next-auth/react'
import { useRouter } from 'next/navigation'
import { trpc } from '@/lib/trpc/client'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Skeleton } from '@/components/ui/skeleton'
import { ApplicantCompetitionTimeline } from '@/components/applicant/competition-timeline'
import { ArrowLeft, FileText } from 'lucide-react'

export default function ApplicantCompetitionPage() {
  const router = useRouter()
  const { data: session } = useSession()
  const { data: myProject, isLoading } = trpc.applicant.getMyDashboard.useQuery(undefined, {
    enabled: !!session,
  })

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

  const hasProject = !!myProject?.project

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold tracking-tight">Competition Timeline</h1>
          <p className="text-muted-foreground mt-1">
            Track your progress through competition rounds
          </p>
        </div>
        <Button variant="ghost" size="sm" onClick={() => router.back()}>
          <ArrowLeft className="mr-2 h-4 w-4" />
          Back
        </Button>
      </div>

      {!hasProject ? (
        <Card>
          <CardContent className="flex flex-col items-center justify-center py-12">
            <FileText className="h-12 w-12 text-muted-foreground/50 mb-4" />
            <h2 className="text-xl font-semibold mb-2">No Active Competition</h2>
            <p className="text-muted-foreground text-center max-w-md">
              You don&apos;t have an active project in any competition yet. Submit your application
              when a competition opens.
            </p>
          </CardContent>
        </Card>
      ) : (
        <div className="grid gap-6 lg:grid-cols-3">
          <div className="lg:col-span-2">
            <ApplicantCompetitionTimeline />
          </div>
          <div className="space-y-4">
            <Card>
              <CardHeader>
                <CardTitle>Timeline Info</CardTitle>
              </CardHeader>
              <CardContent className="space-y-2 text-sm">
                <div className="flex justify-between">
                  <span className="text-muted-foreground">Current Status:</span>
                  <span className="font-medium">
                    {myProject?.currentStatus || 'Unknown'}
                  </span>
                </div>
                {myProject?.project && (
                  <>
                    <div className="flex justify-between">
                      <span className="text-muted-foreground">Project:</span>
                      <span className="font-medium truncate ml-2" title={myProject.project.title}>
                        {myProject.project.title}
                      </span>
                    </div>
                    {myProject.project.submittedAt && (
                      <div className="flex justify-between">
                        <span className="text-muted-foreground">Submitted:</span>
                        <span className="font-medium">
                          {new Date(myProject.project.submittedAt).toLocaleDateString()}
                        </span>
                      </div>
                    )}
                  </>
                )}
              </CardContent>
            </Card>
          </div>
        </div>
      )}
    </div>
  )
}
