'use client'

import { useParams } from 'next/navigation'
import Link from 'next/link'
import type { Route } from 'next'
import { trpc } from '@/lib/trpc/client'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import { Skeleton } from '@/components/ui/skeleton'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { WorkspaceChat } from '@/components/mentor/workspace-chat'
import { FilePromotionPanel } from '@/components/mentor/file-promotion-panel'
import { ArrowLeft, MessageSquare, FileText, Upload } from 'lucide-react'
import { toast } from 'sonner'

export default function MentorWorkspaceDetailPage() {
  const params = useParams()
  const projectId = params.projectId as string

  // Get mentor assignment for this project
  const { data: assignments } = trpc.mentor.getMyProjects.useQuery()
  const assignment = assignments?.find(a => a.projectId === projectId)

  const { data: project, isLoading } = trpc.project.get.useQuery(
    { id: projectId },
    { enabled: !!projectId }
  )

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

  if (!project) {
    return (
      <div className="space-y-6">
        <Button variant="ghost" size="sm" asChild>
          <Link href={'/mentor/workspace' as Route}>
            <ArrowLeft className="mr-2 h-4 w-4" />
            Back
          </Link>
        </Button>
        <Card>
          <CardContent className="flex flex-col items-center justify-center py-12">
            <FileText className="h-12 w-12 text-muted-foreground/50 mb-3" />
            <p className="font-medium">Project not found</p>
          </CardContent>
        </Card>
      </div>
    )
  }

  return (
    <div className="space-y-6">
      <div className="flex items-center gap-4">
        <Button variant="ghost" size="sm" asChild>
          <Link href={'/mentor/workspace' as Route}>
            <ArrowLeft className="mr-2 h-4 w-4" />
            Back
          </Link>
        </Button>
        <div className="flex-1">
          <div className="flex items-center gap-3 flex-wrap">
            <h1 className="text-2xl font-bold tracking-tight">{project.title}</h1>
            {project.status && (
              <Badge variant="secondary">{project.status}</Badge>
            )}
          </div>
          {project.teamName && (
            <p className="text-muted-foreground mt-1">{project.teamName}</p>
          )}
        </div>
      </div>

      <Tabs defaultValue="chat" className="w-full">
        <TabsList className="w-full sm:w-auto grid grid-cols-3 sm:inline-grid">
          <TabsTrigger value="chat" className="flex items-center gap-2">
            <MessageSquare className="h-4 w-4" />
            <span className="hidden sm:inline">Chat</span>
          </TabsTrigger>
          <TabsTrigger value="files" className="flex items-center gap-2">
            <FileText className="h-4 w-4" />
            <span className="hidden sm:inline">Files</span>
          </TabsTrigger>
          <TabsTrigger value="promotion" className="flex items-center gap-2">
            <Upload className="h-4 w-4" />
            <span className="hidden sm:inline">Promotion</span>
          </TabsTrigger>
        </TabsList>

        <TabsContent value="chat" className="mt-6">
          {assignment ? (
            <WorkspaceChat mentorAssignmentId={assignment.id} />
          ) : (
            <Card>
              <CardContent className="text-center py-8">
                <MessageSquare className="h-12 w-12 text-muted-foreground/50 mx-auto mb-3" />
                <p className="text-sm text-muted-foreground">Loading workspace...</p>
              </CardContent>
            </Card>
          )}
        </TabsContent>

        <TabsContent value="files" className="mt-6">
          <Card>
            <CardHeader>
              <CardTitle>Workspace Files</CardTitle>
              <CardDescription>
                Files shared in the mentor workspace
              </CardDescription>
            </CardHeader>
            <CardContent className="text-center py-8">
              <FileText className="h-12 w-12 text-muted-foreground/50 mx-auto mb-3" />
              <p className="text-sm text-muted-foreground">
                File listing feature coming soon
              </p>
            </CardContent>
          </Card>
        </TabsContent>

        <TabsContent value="promotion" className="mt-6">
          {assignment ? (
            <FilePromotionPanel mentorAssignmentId={assignment.id} />
          ) : (
            <Card>
              <CardContent className="text-center py-8">
                <Upload className="h-12 w-12 text-muted-foreground/50 mx-auto mb-3" />
                <p className="text-sm text-muted-foreground">Loading workspace...</p>
              </CardContent>
            </Card>
          )}
        </TabsContent>
      </Tabs>
    </div>
  )
}
