"use server";
import prisma from "@/lib/prisma";
import { revalidatePath } from "next/cache";

export async function loadMockFeatures() {
  await prisma.feature.deleteMany({});
  
  const mockFeatures = [
    { lang: "tr", icon: "Wind", label: "Kitesurf Okulu", sub: "IKO Sertifikalı", order: 1 },
    { lang: "en", icon: "Wind", label: "Kitesurf School", sub: "IKO Certified", order: 1 },
    { lang: "tr", icon: "Anchor", label: "Yoga Platformu", sub: "Nehir Kenarı", order: 2 },
    { lang: "en", icon: "Anchor", label: "Yoga Platform", sub: "Riverside", order: 2 },
    { lang: "tr", icon: "Utensils", label: "Restoran & Bar", sub: "Ege Mutfağı", order: 3 },
    { lang: "en", icon: "Utensils", label: "Restaurant & Bar", sub: "Aegean Cuisine", order: 3 },
    // { lang: "tr", icon: "BedDouble", label: "Butik Odalar", sub: "3 Tip Konaklama", order: 4 },
    // { lang: "en", icon: "BedDouble", label: "Boutique Rooms", sub: "3 Types of Stay", order: 4 },
    { lang: "tr", icon: "Sun", label: "Gün Batımı Terası", sub: "Gökova Manzarası", order: 5 },
    { lang: "en", icon: "Sun", label: "Sunset Terrace", sub: "Gokova View", order: 5 },
  ];

  await prisma.feature.createMany({ data: mockFeatures });
  
  revalidatePath('/admin/features');
  revalidatePath('/tr');
  revalidatePath('/en');
}

export async function addFeature(prevState: any, formData: FormData) {
  try {
    const data = {
      lang: formData.get("lang") as string,
      icon: formData.get("icon") as string,
      label: formData.get("label") as string,
      sub: formData.get("sub") as string,
      order: parseInt(formData.get("order") as string || "0", 10),
    };

    await prisma.feature.create({ data });

    revalidatePath('/admin/features');
    revalidatePath('/tr');
    revalidatePath('/en');
    
    return { success: true, message: "Özellik başarıyla eklendi." };
  } catch (error) {
    return { error: "Özellik eklenirken bir hata oluştu." };
  }
}

export async function updateFeature(prevState: any, formData: FormData) {
  try {
    const id = parseInt(formData.get("id") as string, 10);
    const data = {
      lang: formData.get("lang") as string,
      icon: formData.get("icon") as string,
      label: formData.get("label") as string,
      sub: formData.get("sub") as string,
      order: parseInt(formData.get("order") as string || "0", 10),
    };

    await prisma.feature.update({
      where: { id },
      data
    });

    revalidatePath('/admin/features');
    revalidatePath('/tr');
    revalidatePath('/en');
    
    return { success: true, message: "Özellik başarıyla güncellendi." };
  } catch (error) {
    return { error: "Özellik güncellenirken bir hata oluştu." };
  }
}

export async function deleteFeature(id: number) {
  try {
    await prisma.feature.delete({ where: { id } });
    revalidatePath('/admin/features');
    revalidatePath('/tr');
    revalidatePath('/en');
    return { success: true, message: "Özellik silindi." };
  } catch (error) {
    return { error: "Silme işlemi başarısız." };
  }
}
