"use server";

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

export type SettingsActionState = {
  success?: boolean;
  message?: string;
} | null;

export async function updateSettings(prevState: SettingsActionState, formData: FormData): Promise<SettingsActionState> {
  const title = formData.get("title") as string;
  const email = formData.get("email") as string;
  const phone = formData.get("phone") as string;
  const address = formData.get("address") as string;
  const instagram = formData.get("instagram") as string;

  await prisma.siteSettings.upsert({
    where: { id: 1 },
    update: { title, email, phone, address, instagram },
    create: {
      id: 1,
      title,
      email,
      phone,
      address,
      instagram
    }
  });
  
  revalidatePath('/admin/settings');
  revalidatePath('/'); 

  return {
    success: true,
    message: "Ayarlar başarıyla veritabanına kaydedildi!"
  };
}
