import multer from "multer";
import path from "path";
import crypto from "crypto";
import { STATIC_SOUNDS_DIR } from "@/core/storage-paths";

const storage = multer.diskStorage({
    destination: (_req, _file, cb) => cb(null, STATIC_SOUNDS_DIR),
    filename: (_req, file, cb) => {
        const ext = path.extname(file.originalname).toLowerCase() || ".mp3";
        cb(null, `${crypto.randomUUID()}${ext}`);
    },
});

const ALLOWED_AUDIO_MIMES = [
    "audio/mpeg",
    "audio/mp3",
    "audio/wav",
    "audio/wave",
    "audio/x-wav",
    "audio/ogg",
    "audio/webm",
    "audio/aac",
    "audio/mp4",
];

export const uploadSound = multer({
    storage,
    fileFilter: (_req, file, cb) => {
        if (ALLOWED_AUDIO_MIMES.includes(file.mimetype)) {
            cb(null, true);
        } else {
            cb(new Error("Format non supporté. Formats acceptés : MP3, WAV, OGG, AAC, WebM"));
        }
    },
    limits: { fileSize: 5 * 1024 * 1024 }, // 5 Mo max
});

