"use server"

import { revalidatePath } from "next/cache"
import { getTranslations } from "next-intl/server"
import { requirePermission } from "@/shared/lib/auth-guard"
import { handleActionError } from "@/shared/lib/action-utils"
import { Permissions } from "@/shared/types/permissions"

import type { ActionState } from "@/shared/types/action-state"
import {
  createAdminClass,
  deleteAdminClass,
  setClassSubjectTeachers,
  updateAdminClass,
} from "./data-access"
import {
  CreateAdminClassSchema,
  UpdateAdminClassSchema,
  DeleteAdminClassSchema,
} from "./schema"
import { parseSubjectTeachers } from "./actions-shared"

export async function createAdminClassAction(
  prevState: ActionState<string> | undefined,
  formData: FormData
): Promise<ActionState<string>> {
  try {
    await requirePermission(Permissions.CLASS_CREATE)
    const t = await getTranslations("classes")

    const parsed = CreateAdminClassSchema.safeParse({
      name: formData.get("name"),
      grade: formData.get("grade"),
      teacherId: formData.get("teacherId"),
      schoolName: formData.get("schoolName"),
      schoolId: formData.get("schoolId"),
      gradeId: formData.get("gradeId"),
      homeroom: formData.get("homeroom"),
      room: formData.get("room"),
    })
    if (!parsed.success) {
      return { success: false, message: t("actions.classNameGradeTeacherRequired") }
    }

    const { name, grade, teacherId, schoolName, schoolId, gradeId, homeroom, room } = parsed.data

    try {
      const id = await createAdminClass({
        schoolName: schoolName ?? null,
        schoolId: schoolId ?? null,
        name,
        grade,
        gradeId: gradeId ?? null,
        teacherId,
        homeroom: homeroom ?? null,
        room: room ?? null,
      })
      revalidatePath("/admin/school/classes")
      revalidatePath("/teacher/classes/my")
      revalidatePath("/teacher/classes/students")
      revalidatePath("/teacher/classes/schedule")
      return { success: true, message: t("actions.classCreated"), data: id }
    } catch (error) {
      return handleActionError(error)
    }
  } catch (e) {
    return handleActionError(e)
  }
}

export async function updateAdminClassAction(
  classId: string,
  prevState: ActionState | undefined,
  formData: FormData
): Promise<ActionState> {
  try {
    await requirePermission(Permissions.CLASS_UPDATE)
    const t = await getTranslations("classes")

    const parsed = UpdateAdminClassSchema.safeParse({
      classId,
      schoolName: formData.get("schoolName"),
      schoolId: formData.get("schoolId"),
      name: formData.get("name"),
      grade: formData.get("grade"),
      gradeId: formData.get("gradeId"),
      teacherId: formData.get("teacherId"),
      homeroom: formData.get("homeroom"),
      room: formData.get("room"),
    })
    if (!parsed.success) {
      return { success: false, message: t("actions.missingClassId") }
    }

    const { classId: validatedClassId, schoolName, schoolId, name, grade, gradeId, teacherId, homeroom, room } = parsed.data
    const subjectTeachers = await parseSubjectTeachers(formData.get("subjectTeachers"))

    try {
      await updateAdminClass(validatedClassId, {
        schoolName: schoolName ?? undefined,
        schoolId: schoolId ?? undefined,
        name: name ?? undefined,
        grade: grade ?? undefined,
        gradeId: gradeId ?? undefined,
        teacherId: teacherId ?? undefined,
        homeroom: homeroom ?? undefined,
        room: room ?? undefined,
      })

      if (subjectTeachers) {
        await setClassSubjectTeachers({
          classId: validatedClassId,
          assignments: subjectTeachers,
        })
      }

      revalidatePath("/admin/school/classes")
      revalidatePath("/teacher/classes/my")
      revalidatePath("/teacher/classes/students")
      revalidatePath("/teacher/classes/schedule")
      return { success: true, message: t("actions.classUpdated") }
    } catch (error) {
      return handleActionError(error)
    }
  } catch (e) {
    return handleActionError(e)
  }
}

export async function deleteAdminClassAction(classId: string): Promise<ActionState> {
  try {
    await requirePermission(Permissions.CLASS_DELETE)
    const t = await getTranslations("classes")

    const parsed = DeleteAdminClassSchema.safeParse({ classId })
    if (!parsed.success) {
      return { success: false, message: t("actions.missingClassId") }
    }

    try {
      await deleteAdminClass(parsed.data.classId)
      revalidatePath("/admin/school/classes")
      revalidatePath("/teacher/classes/my")
      revalidatePath("/teacher/classes/students")
      revalidatePath("/teacher/classes/schedule")
      return { success: true, message: t("actions.classDeleted") }
    } catch (error) {
      return handleActionError(error)
    }
  } catch (e) {
    return handleActionError(e)
  }
}
