'use server';

import { redirect } from 'next/navigation';
import { prisma } from '@/lib/prisma';
import { SITE_SETTINGS_ID } from '@/lib/settings';

export async function setMaintenance(formData: FormData) {
  const enable = String(formData.get('enable') ?? '') === '1';
  const message = String(formData.get('message') ?? '').trim();

  await prisma.siteSetting.upsert({
    where: { id: SITE_SETTINGS_ID },
    create: {
      id: SITE_SETTINGS_ID,
      maintenanceMode: enable,
      maintenanceMessage: message || null,
    },
    update: {
      maintenanceMode: enable,
      maintenanceMessage: message || null,
    },
  });

  redirect('/admin/maintenance');
}
