import prisma from "@/lib/prisma";
import { Prisma } from "@prisma/client";
import { Context } from "elysia";
import path from "path";
import fs from "fs/promises";

type FormUpdate = Prisma.PotensiDesaGetPayload<{
    select: {
        id: true;
        name: true;
        deskripsi: true;
        kategori: true;
        imageId: true;
        content: true;
    };
}>;

export default async function potensiDesaUpdate(context: Context) {
    try {
        const id = context.params?.id as string;
    const body = (await context.body) as Omit<FormUpdate, "id">;

    const {
        name,
        deskripsi,
        kategori,
        imageId,
        content,
        } = body;

        if (!id) {
            return new Response(
                JSON.stringify({ success: false, message: "ID tidak ditemukan" }),
                { status: 400, headers: { 'Content-Type': 'application/json' } }
            )
        }

        const existing = await prisma.potensiDesa.findUnique({
            where: { id },
            include: {
                image: true,
            }
        });

        if (!existing) {
            return new Response(
                JSON.stringify({ success: false, message: "Potensi Desa tidak ditemukan"}),
                { status: 404, headers: { 'Content-Type': 'application/json' } }
            )
        }

        if (existing.imageId && existing.imageId !== imageId) {
            const oldImage = existing.image;
            if (oldImage) {
                try {
                    const filePath = path.join(oldImage.path, oldImage.name);
                    await fs.unlink(filePath);
                    await prisma.fileStorage.delete({
                        where: { id: oldImage.id },
                    });
                } catch (error) {
                    console.error("Gagal hapus gambar lama:", error);
                }
            }
        }

        const updated = await prisma.potensiDesa.update({
            where: { id },
            data: {
                name,
                deskripsi,
                kategori,
                imageId,
                content,
            },
        });

        return new Response(
            JSON.stringify({
                success: true,
                message: "Potensi Desa berhasil diupdate",
                data: updated,
            }),
            { status: 200, headers: { 'Content-Type': 'application/json' } }
        );
    } catch (error) {
        console.error("Error updating potensi desa:", error);
        return new Response(
            JSON.stringify({
                success: false,
                message: "Terjadi kesalahan saat mengupdate potensi desa",
            }),
            { status: 500, headers: { 'Content-Type': 'application/json' } }
        );
    }
    
}
    
