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

export async function loadMockEvents() {
  await prisma.event.deleteMany({});
  
  const mockEvents = [
    {
      lang: "tr",
      title: "Mahmut Orhan - Sunset Session",
      description: "Dünyaca ünlü DJ Mahmut Orhan, unutulmaz bir gün batımı performansı için Kite Beach Akyaka'da! Ege'nin muhteşem manzarasına karşı elektronik müziğin ritmini hissedeceğiniz bu özel geceyi kaçırmayın.",
      date: "15 Ağustos 2026",
      time: "18:00",
      location: "Beach Club",
      image: "https://images.unsplash.com/photo-1516450360452-9312f5e86fc7?q=80&w=2832&auto=format&fit=crop",
      badge: "Yaklaşan Etkinlik"
    },
    {
      lang: "en",
      title: "Mahmut Orhan - Sunset Session",
      description: "World famous DJ Mahmut Orhan is at Kite Beach Akyaka for an unforgettable sunset performance! Do not miss this special night where you will feel the rhythm of electronic music against the magnificent view of the Aegean.",
      date: "15 August 2026",
      time: "18:00",
      location: "Beach Club",
      image: "https://images.unsplash.com/photo-1516450360452-9312f5e86fc7?q=80&w=2832&auto=format&fit=crop",
      badge: "Upcoming Event"
    }
  ];

  await prisma.event.createMany({ data: mockEvents });
  
  revalidatePath('/admin/events');
  revalidatePath('/tr');
  revalidatePath('/en');
}

export async function addEvent(prevState: any, formData: FormData) {
  try {
    const data = {
      lang: formData.get("lang") as string,
      title: formData.get("title") as string,
      description: formData.get("description") as string,
      date: formData.get("date") as string,
      time: formData.get("time") as string,
      location: formData.get("location") as string,
      image: formData.get("image") as string,
      badge: formData.get("badge") as string || null,
    };

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

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

export async function updateEvent(prevState: any, formData: FormData) {
  try {
    const id = parseInt(formData.get("id") as string, 10);
    const data = {
      lang: formData.get("lang") as string,
      title: formData.get("title") as string,
      description: formData.get("description") as string,
      date: formData.get("date") as string,
      time: formData.get("time") as string,
      location: formData.get("location") as string,
      image: formData.get("image") as string,
      badge: formData.get("badge") as string || null,
    };

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

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

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