import { Context } from "elysia";
import prisma from "@/lib/prisma";
import minio, { MINIO_BUCKET } from "@/lib/minio";

const musikDelete = async (context: Context) => {
  const { id } = context.params as { id: string };

  const musik = await prisma.musikDesa.findUnique({
    where: { id },
    include: { audioFile: true, coverImage: true },
  });

  if (!musik) return { status: 404, body: "Musik tidak ditemukan" };

  // 1. HAPUS MUSIK DULU
  await prisma.musikDesa.delete({ where: { id } });

  // 2. HAPUS FILE AUDIO (jika ada)
  if (musik.audioFile) {
    try {
      await minio.removeObject(MINIO_BUCKET, `${musik.audioFile.path}/${musik.audioFile.name}`);

      await prisma.fileStorage.delete({
        where: { id: musik.audioFile.id },
      });
    } catch (error) {
      console.error("Error deleting audio file:", error);
    }
  }

  // 3. HAPUS FILE COVER (jika ada)
  if (musik.coverImage) {
    try {
      await minio.removeObject(MINIO_BUCKET, `${musik.coverImage.path}/${musik.coverImage.name}`);

      await prisma.fileStorage.delete({
        where: { id: musik.coverImage.id },
      });
    } catch (error) {
      console.error("Error deleting cover image:", error);
    }
  }

  return {
    success: true,
    message: "Musik dan file terkait berhasil dihapus",
  };
};

export default musikDelete;
