import {
  BookOpen,
  Layers,
  Wand2,
  GitBranch,
  ClipboardList,
  FolderOpen,
  Users,
  GraduationCap,
  FileText,
  Ticket,
  Building2,
  Sparkles,
} from "lucide-react";

import { QuickSetupWizard, type WizardStep, type QuickCreate } from "@/components/QuickSetupWizard";
import { examsService } from "@/services/exams.service";
import { assignmentsService } from "@/services/assignments.service";

/**
 * Admin smart-setup wizard.
 *
 * Surfaces the recommended order for standing up an exam-ready platform:
 * rubric → structure → generate → approve → assign. Each step deep-links
 * into the existing creation page and auto-ticks when the backend confirms
 * at least one record exists, so coming back here after a sub-task shows
 * visible progress.
 *
 * "Other quick creates" covers the most common ad-hoc actions (new course,
 * upload resource, add user, etc.) so admins can jump straight in without
 * hunting through the sidebar.
 */

const steps: WizardStep[] = [
  {
    id: "rubric",
    titleKey: "quickSetup.admin.step1.title",
    descriptionKey: "quickSetup.admin.step1.description",
    helpKey: "quickSetup.admin.step1.help",
    to: "/admin/rubrics",
    icon: BookOpen,
    check: async () => {
      const res = await examsService.listRubrics({ page: 1, size: 1 });
      return (res.items?.length ?? 0) > 0;
    },
  },
  {
    id: "structure",
    titleKey: "quickSetup.admin.step2.title",
    descriptionKey: "quickSetup.admin.step2.description",
    helpKey: "quickSetup.admin.step2.help",
    to: "/admin/exam-structures",
    icon: Layers,
    check: async () => {
      const res = await examsService.listStructures({ page: 1, size: 1 });
      return (res.items?.length ?? 0) > 0;
    },
  },
  {
    id: "generate",
    titleKey: "quickSetup.admin.step3.title",
    descriptionKey: "quickSetup.admin.step3.description",
    helpKey: "quickSetup.admin.step3.help",
    to: "/admin/generation",
    icon: Wand2,
    // "Is there at least one exam of any module?" — cheapest existence
    // check; the writing module is a common default.
    check: async () => {
      const res = await examsService.list("writing", { page: 1, size: 1 });
      return (res.items?.length ?? 0) > 0;
    },
  },
  {
    id: "approve",
    titleKey: "quickSetup.admin.step4.title",
    descriptionKey: "quickSetup.admin.step4.description",
    helpKey: "quickSetup.admin.step4.help",
    to: "/admin/approval-workflows",
    icon: GitBranch,
    // No cheap "any workflow exists" endpoint — leave this one uncheckable
    // so it stays neutral and always visible in the flow.
  },
  {
    id: "assign",
    titleKey: "quickSetup.admin.step5.title",
    descriptionKey: "quickSetup.admin.step5.description",
    helpKey: "quickSetup.admin.step5.help",
    to: "/admin/assignments",
    icon: ClipboardList,
    check: async () => {
      const res = await assignmentsService.listSchedules({ page: 1, size: 1 });
      return (res.items?.length ?? 0) > 0;
    },
  },
];

const quickCreates: QuickCreate[] = [
  {
    id: "course",
    titleKey: "quickSetup.admin.quick.course.title",
    descriptionKey: "quickSetup.admin.quick.course.description",
    to: "/admin/courses",
    icon: BookOpen,
  },
  {
    id: "resource",
    titleKey: "quickSetup.admin.quick.resource.title",
    descriptionKey: "quickSetup.admin.quick.resource.description",
    to: "/admin/resources",
    icon: FolderOpen,
  },
  {
    id: "student",
    titleKey: "quickSetup.admin.quick.student.title",
    descriptionKey: "quickSetup.admin.quick.student.description",
    to: "/admin/students",
    icon: Users,
  },
  {
    id: "teacher",
    titleKey: "quickSetup.admin.quick.teacher.title",
    descriptionKey: "quickSetup.admin.quick.teacher.description",
    to: "/admin/teachers",
    icon: GraduationCap,
  },
  {
    id: "classroom",
    titleKey: "quickSetup.admin.quick.classroom.title",
    descriptionKey: "quickSetup.admin.quick.classroom.description",
    to: "/admin/classrooms",
    icon: Building2,
  },
  {
    id: "exam-session",
    titleKey: "quickSetup.admin.quick.examSession.title",
    descriptionKey: "quickSetup.admin.quick.examSession.description",
    to: "/admin/exam-sessions",
    icon: FileText,
  },
  {
    id: "custom-exam",
    titleKey: "quickSetup.admin.quick.customExam.title",
    descriptionKey: "quickSetup.admin.quick.customExam.description",
    to: "/admin/exams/new",
    icon: Sparkles,
  },
  {
    id: "ticket",
    titleKey: "quickSetup.admin.quick.ticket.title",
    descriptionKey: "quickSetup.admin.quick.ticket.description",
    to: "/admin/tickets",
    icon: Ticket,
  },
];

export default function AdminQuickSetup() {
  return (
    <QuickSetupWizard
      titleKey="quickSetup.adminTitle"
      subtitleKey="quickSetup.adminSubtitle"
      steps={steps}
      quickCreates={quickCreates}
    />
  );
}
