import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { useAssignments } from "@/hooks/queries";
import { Link } from "react-router-dom";
import AiCreationAssistant from "@/components/ai/AiCreationAssistant";
import AiGeneratorModal from "@/components/ai/AiGeneratorModal";
import AiTipBanner from "@/components/ai/AiTipBanner";

export default function TeacherAssignments() {
  const { data: assignmentsData, isLoading } = useAssignments();
  const assignments = assignmentsData?.items ?? [];
  const submissions: unknown[] = [];
  if (isLoading) return <div className="flex items-center justify-center min-h-[400px]"><div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary" /></div>;

  const teacherAssignments = assignments;
  const pendingSubs = (submissions as { id: string; studentName: string; submittedAt: string; status: string; feedback?: string; grade?: number }[]).filter(s => s.status === "pending");
  const gradedSubs = (submissions as { id: string; studentName: string; submittedAt: string; status: string; feedback?: string; grade?: number }[]).filter(s => s.status === "graded");

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold">Assignments</h1>
          <p className="text-muted-foreground">Manage and grade student assignments.</p>
        </div>
        <div className="flex gap-2">
          <AiCreationAssistant type="assignment" />
          <AiGeneratorModal />
        </div>
      </div>

      <AiTipBanner context="teacher-assignments" variant="insight" />

      <Tabs defaultValue="all">
        <TabsList>
          <TabsTrigger value="all">All ({teacherAssignments.length})</TabsTrigger>
          <TabsTrigger value="pending">Pending Review ({pendingSubs.length})</TabsTrigger>
          <TabsTrigger value="graded">Graded ({gradedSubs.length})</TabsTrigger>
        </TabsList>

        <TabsContent value="all" className="mt-4 space-y-3">
          {teacherAssignments.map(a => (
            <Link to={`/teacher/assignments/${a.id}`} key={a.id}>
              <Card className="hover:bg-muted/30 transition-colors">
                <CardContent className="pt-4 flex items-center justify-between">
                  <div><p className="font-medium text-sm">{a.title}</p><p className="text-xs text-muted-foreground">{a.entity_name} · Due: {a.end_date}</p></div>
                  <div className="flex items-center gap-2">
                    <Badge variant="outline" className="capitalize text-xs">{a.state}</Badge>
                    <Badge variant={a.completed_count >= a.assignee_count && a.assignee_count > 0 ? "default" : "secondary"} className="capitalize">{a.state}</Badge>
                  </div>
                </CardContent>
              </Card>
            </Link>
          ))}
        </TabsContent>

        <TabsContent value="pending" className="mt-4 space-y-3">
          {pendingSubs.map(s => (
            <Card key={s.id}>
              <CardContent className="pt-4 flex items-center justify-between">
                <div><p className="font-medium text-sm">{s.studentName}</p><p className="text-xs text-muted-foreground">Submitted: {s.submittedAt}</p></div>
                <Badge variant="secondary">Pending</Badge>
              </CardContent>
            </Card>
          ))}
        </TabsContent>

        <TabsContent value="graded" className="mt-4 space-y-3">
          {gradedSubs.map(s => (
            <Card key={s.id}>
              <CardContent className="pt-4 flex items-center justify-between">
                <div><p className="font-medium text-sm">{s.studentName}</p><p className="text-xs text-muted-foreground">{s.feedback}</p></div>
                <span className="font-bold text-primary">{s.grade}</span>
              </CardContent>
            </Card>
          ))}
        </TabsContent>
      </Tabs>
    </div>
  );
}
