"use server";

import { prisma } from "@/core/db/prisma";
import { verifySession } from "@/core/security/session";
import { revalidatePath } from "next/cache";
import { z } from "zod";

const skillSchema = z.object({
  name: z.string().min(1, "Name is required"),
  category: z.enum(["backend", "infra", "frontend", "mobile"]).refine(
    (v) => ["backend", "infra", "frontend", "mobile"].includes(v),
    { message: "Invalid category" }
  ),
  iconName: z.string().optional(),
  sortOrder: z.coerce.number().int().min(0).default(0),
});

export async function createSkillAction(prevState: any, formData: FormData) {
  const session = await verifySession();
  if (!session) return { success: false, message: "Unauthorized" };

  const data = {
    name: formData.get("name") as string,
    category: formData.get("category") as string,
    iconName: formData.get("iconName") as string,
    sortOrder: formData.get("sortOrder") as string,
  };

  const validation = skillSchema.safeParse(data);
  if (!validation.success) {
    return { success: false, message: validation.error.issues[0].message };
  }

  try {
    await prisma.skill.create({
      data: {
        name: validation.data.name,
        category: validation.data.category,
        iconName: validation.data.iconName || null,
        sortOrder: validation.data.sortOrder,
      },
    });
    revalidatePath("/admin/dashboard/skills");
    revalidatePath("/");
    return { success: true };
  } catch (error) {
    console.error(error);
    return { success: false, message: "Failed to create skill" };
  }
}

export async function updateSkillAction(
  id: string,
  prevState: any,
  formData: FormData
) {
  const session = await verifySession();
  if (!session) return { success: false, message: "Unauthorized" };

  const data = {
    name: formData.get("name") as string,
    category: formData.get("category") as string,
    iconName: formData.get("iconName") as string,
    sortOrder: formData.get("sortOrder") as string,
  };

  const validation = skillSchema.safeParse(data);
  if (!validation.success) {
    return { success: false, message: validation.error.issues[0].message };
  }

  try {
    await prisma.skill.update({
      where: { id },
      data: {
        name: validation.data.name,
        category: validation.data.category,
        iconName: validation.data.iconName || null,
        sortOrder: validation.data.sortOrder,
      },
    });
    revalidatePath("/admin/dashboard/skills");
    revalidatePath("/");
    return { success: true };
  } catch (error) {
    return { success: false, message: "Failed to update skill" };
  }
}

export async function deleteSkillAction(id: string) {
  const session = await verifySession();
  if (!session) return { success: false, message: "Unauthorized" };

  try {
    await prisma.skill.delete({ where: { id } });
    revalidatePath("/admin/dashboard/skills");
    revalidatePath("/");
    return { success: true };
  } catch (error) {
    return { success: false, message: "Failed to delete skill" };
  }
}
