'use server';

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

export async function createOrbitItem(formData: FormData) {
    const hargaRaw = formData.get('harga') as string;
    const payload = {
        judul: formData.get('judul') as string,
        deskripsi: (formData.get('deskripsi') as string) || null,
        harga: parseInt(hargaRaw) || 0,
        kondisi: formData.get('kondisi') as string,
        kategori: formData.get('kategori') as string,
        foto_url: (formData.get('foto_url') as string) || null,
        penjual_nama: formData.get('penjual_nama') as string,
        penjual_instagram: (formData.get('penjual_instagram') as string).replace(/^@/, ''),
        is_terjual: false,
        is_active: formData.get('is_active') === 'true',
    };

    if (!payload.judul || !payload.penjual_nama || !payload.penjual_instagram) {
        return { error: 'Judul, Nama Penjual, dan Instagram wajib diisi.' };
    }

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

export async function updateOrbitItem(id: string, formData: FormData) {
    const hargaRaw = formData.get('harga') as string;
    const payload = {
        judul: formData.get('judul') as string,
        deskripsi: (formData.get('deskripsi') as string) || null,
        harga: parseInt(hargaRaw) || 0,
        kondisi: formData.get('kondisi') as string,
        kategori: formData.get('kategori') as string,
        foto_url: (formData.get('foto_url') as string) || null,
        penjual_nama: formData.get('penjual_nama') as string,
        penjual_instagram: (formData.get('penjual_instagram') as string).replace(/^@/, ''),
        is_terjual: formData.get('is_terjual') === 'true',
        is_active: formData.get('is_active') === 'true',
    };

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

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

export async function toggleOrbitTerjual(id: string, isTerjual: boolean) {
    try {
        await prisma.orbitItem.update({
            where: { id },
            data: { is_terjual: isTerjual }
        });
        revalidatePath('/orbit');
        revalidatePath('/admin/orbit');
        return { success: true };
    } catch (e: any) {
        return { error: e.message };
    }
}

export async function getAllOrbitItemsAdmin() {
    try {
        const data = await prisma.orbitItem.findMany({
            orderBy: { created_at: 'desc' }
        });
        return data || [];
    } catch {
        return [];
    }
}
