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

export async function updateAboutSection(prevState: any, formData: FormData) {
  try {
    const lang = formData.get("lang") as string;
    const badge = formData.get("badge") as string;
    const titleLine1 = formData.get("titleLine1") as string;
    const titleLine2 = formData.get("titleLine2") as string;
    const titleHighlight = formData.get("titleHighlight") as string;
    const description1 = formData.get("description1") as string;
    const description2 = formData.get("description2") as string;
    const image = formData.get("image") as string;
    const statsGuestCount = formData.get("statsGuestCount") as string;
    const statsGuestLabel = formData.get("statsGuestLabel") as string;
    const statsYearCount = formData.get("statsYearCount") as string;
    const statsYearLabel = formData.get("statsYearLabel") as string;
    const badgeIkoTitle = formData.get("badgeIkoTitle") as string;
    const badgeIkoLabel = formData.get("badgeIkoLabel") as string;

    await prisma.aboutSection.upsert({
      where: { lang },
      update: {
        badge,
        titleLine1,
        titleLine2,
        titleHighlight,
        description1,
        description2,
        image,
        statsGuestCount,
        statsGuestLabel,
        statsYearCount,
        statsYearLabel,
        badgeIkoTitle,
        badgeIkoLabel
      },
      create: {
        lang,
        badge,
        titleLine1,
        titleLine2,
        titleHighlight,
        description1,
        description2,
        image,
        statsGuestCount,
        statsGuestLabel,
        statsYearCount,
        statsYearLabel,
        badgeIkoTitle,
        badgeIkoLabel
      }
    });

    revalidatePath('/admin/about');
    revalidatePath('/tr');
    revalidatePath('/en');
    
    return { success: true, message: "Hakkımızda bölümü başarıyla güncellendi." };
  } catch (error) {
    return { error: "Güncellenirken bir hata oluştu." };
  }
}
