'use server';

import { prisma } from './prisma';
import { revalidatePath } from 'next/cache';

export async function createFluksItem(formData: FormData) {
    const payload = {
        nama: formData.get('nama') as string,
        deskripsi: (formData.get('deskripsi') as string) || null,
        harga: parseInt(formData.get('harga') as string) || 0,
        kategori: formData.get('kategori') as string,
        foto_url: (formData.get('foto_url') as string) || null,
        link_order: (formData.get('link_order') as string) || null,
        tipe_link: formData.get('tipe_link') as string,
        stok: formData.get('stok') as string,
        badge: (formData.get('badge') as string) || null,
        is_active: formData.get('is_active') === 'true',
        urutan: parseInt(formData.get('urutan') as string) || 0,
    };

    if (!payload.nama) return { error: 'Nama produk wajib diisi.' };

    try {
        await prisma.fluksItem.create({ data: payload });
        revalidatePath('/fluks');
        revalidatePath('/admin/fluks');
        return { success: true };
    } catch (e: any) {
        return { error: e.message };
    }
}

export async function updateFluksItem(id: string, formData: FormData) {
    const payload = {
        nama: formData.get('nama') as string,
        deskripsi: (formData.get('deskripsi') as string) || null,
        harga: parseInt(formData.get('harga') as string) || 0,
        kategori: formData.get('kategori') as string,
        foto_url: (formData.get('foto_url') as string) || null,
        link_order: (formData.get('link_order') as string) || null,
        tipe_link: formData.get('tipe_link') as string,
        stok: formData.get('stok') as string,
        badge: (formData.get('badge') as string) || null,
        is_active: formData.get('is_active') === 'true',
        urutan: parseInt(formData.get('urutan') as string) || 0,
    };

    try {
        await prisma.fluksItem.update({ where: { id }, data: payload });
        revalidatePath('/fluks');
        revalidatePath('/admin/fluks');
        return { success: true };
    } catch (e: any) {
        return { error: e.message };
    }
}

export async function deleteFluksItem(id: string) {
    try {
        await prisma.fluksItem.delete({ where: { id } });
        revalidatePath('/fluks');
        revalidatePath('/admin/fluks');
        return { success: true };
    } catch (e: any) {
        return { error: e.message };
    }
}

export async function getAllFluksItemsAdmin() {
    try {
        const data = await prisma.fluksItem.findMany({
            orderBy: [
                { urutan: 'asc' },
                { created_at: 'desc' }
            ]
        });
        return data || [];
    } catch {
        return [];
    }
}

export async function getFluksConfigAdmin() {
    try {
        return await prisma.fluksConfig.findFirst();
    } catch {
        return null;
    }
}

export async function updateFluksConfig(id: string, formData: FormData) {
    const payload = {
        form_order_url: formData.get('form_order_url') as string,
        catatan: (formData.get('catatan') as string) || null,
    };

    if (!payload.form_order_url) return { error: 'URL form tidak boleh kosong.' };

    try {
        await prisma.fluksConfig.update({ where: { id }, data: payload });
        revalidatePath('/fluks');
        revalidatePath('/admin/fluks');
        return { success: true };
    } catch (e: any) {
        return { error: e.message };
    }
}
