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

export async function loadMockActivities() {
  await prisma.activity.deleteMany({});
  
  const mockActivities = [
    {
      lang: "tr",
      title: "Kitesurf",
      description: "Başlangıçtan ileri seviyeye. Akyaka'nın ünlü termik rüzgarında uçun.",
      price: "₺2.000/Saat",
      image: "https://images.unsplash.com/photo-1526367790999-0150786686a2?q=80&w=2940&auto=format&fit=crop",
      badge: "IKO Sertifikalı",
      order: 1
    },
    {
      lang: "en",
      title: "Kitesurf",
      description: "From beginner to advanced. Fly in Akyaka's famous thermal wind.",
      price: "₺2.000/Hour",
      image: "https://images.unsplash.com/photo-1526367790999-0150786686a2?q=80&w=2940&auto=format&fit=crop",
      badge: "IKO Certified",
      order: 1
    },
    {
      lang: "tr",
      title: "Yoga",
      description: "Güne nehir kenarında huzur dolu bir yoga seansıyla başlayın.",
      price: "₺400/Seans",
      image: "https://images.unsplash.com/photo-1544367567-0f2fcb009e0b?q=80&w=2940&auto=format&fit=crop",
      badge: null,
      order: 2
    },
    {
      lang: "en",
      title: "Yoga",
      description: "Start the day with a peaceful yoga session by the river.",
      price: "₺400/Session",
      image: "https://images.unsplash.com/photo-1544367567-0f2fcb009e0b?q=80&w=2940&auto=format&fit=crop",
      badge: null,
      order: 2
    },
    {
      lang: "tr",
      title: "Stand-Up Paddle",
      description: "Azmak nehrinin berrak sularında eşsiz bir keşif.",
      price: "₺300/Saat",
      image: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?q=80&w=2940&auto=format&fit=crop",
      badge: null,
      order: 3
    },
    {
      lang: "en",
      title: "Stand-Up Paddle",
      description: "A unique discovery in the crystal clear waters of Azmak River.",
      price: "₺300/Hour",
      image: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?q=80&w=2940&auto=format&fit=crop",
      badge: null,
      order: 3
    },
    {
      lang: "tr",
      title: "Bisiklet Kiralama",
      description: "Akyaka Köy Turu ile doğanın tadını çıkarın.",
      price: "₺250/Gün",
      image: "https://images.unsplash.com/photo-1511994298241-608e28f14fde?q=80&w=2940&auto=format&fit=crop",
      badge: null,
      order: 4
    },
    {
      lang: "en",
      title: "Bicycle Rental",
      description: "Enjoy nature with an Akyaka Village Tour.",
      price: "₺250/Day",
      image: "https://images.unsplash.com/photo-1511994298241-608e28f14fde?q=80&w=2940&auto=format&fit=crop",
      badge: null,
      order: 4
    }
  ];

  await prisma.activity.createMany({ data: mockActivities });
  
  revalidatePath('/admin/activities');
  revalidatePath('/tr');
  revalidatePath('/en');
}

export async function addActivity(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,
      price: (formData.get("price") as string) || "",
      image: formData.get("image") as string,
      badge: formData.get("badge") as string || null,
      order: parseInt(formData.get("order") as string || "0", 10),
    };

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

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

export async function updateActivity(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,
      price: (formData.get("price") as string) || "",
      image: formData.get("image") as string,
      badge: formData.get("badge") as string || null,
      order: parseInt(formData.get("order") as string || "0", 10),
    };

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

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

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