import app from '@adonisjs/core/services/app';
import Music from '#models/music';
import fs from 'node:fs';
export default class AdminMusicController {
    async index({ view }) {
        const musics = await Music.all();
        return view.render('admin/music/index', { musics });
    }
    async create({ view }) {
        return view.render('admin/music/form', { music: null });
    }
    async store({ request, response, session }) {
        const { name } = request.only(['name']);
        const musicFile = request.file('file', {
            size: '10mb',
            extnames: ['mp3', 'wav', 'ogg'],
        });
        if (!musicFile) {
            session.flashErrors({ error: 'Veuillez télécharger un fichier audio.' });
            return response.redirect().back();
        }
        await musicFile.move(app.makePath('public/uploads/music'), {
            name: `${new Date().getTime()}_${musicFile.clientName}`
        });
        const url = `/uploads/music/${musicFile.fileName}`;
        await Music.create({ name, url });
        session.flash({ success: 'Musique ajoutée avec succès.' });
        return response.redirect('/admin/music');
    }
    async edit({ view, params, response }) {
        const music = await Music.find(params.id);
        if (!music) {
            return response.notFound();
        }
        return view.render('admin/music/form', { music });
    }
    async update({ request, params, response, session }) {
        const music = await Music.find(params.id);
        if (!music) {
            return response.notFound();
        }
        const { name } = request.only(['name']);
        const musicFile = request.file('file', {
            size: '10mb',
            extnames: ['mp3', 'wav', 'ogg'],
        });
        let url = music.url;
        if (musicFile) {
            const oldPath = app.makePath('public' + music.url);
            if (fs.existsSync(oldPath)) {
                try {
                    fs.unlinkSync(oldPath);
                }
                catch (err) {
                    console.error('Failed to delete old music file', err);
                }
            }
            await musicFile.move(app.makePath('public/uploads/music'), {
                name: `${new Date().getTime()}_${musicFile.clientName}`
            });
            url = `/uploads/music/${musicFile.fileName}`;
        }
        await music.merge({ name, url }).save();
        session.flash({ success: 'Musique modifiée avec succès.' });
        return response.redirect('/admin/music');
    }
    async destroy({ params, response, session }) {
        const music = await Music.find(params.id);
        if (!music) {
            return response.notFound();
        }
        const absolutePath = app.makePath('public' + music.url);
        if (fs.existsSync(absolutePath)) {
            try {
                fs.unlinkSync(absolutePath);
            }
            catch (err) {
                console.error('Failed to delete music file', err);
            }
        }
        await music.delete();
        session.flash({ success: 'Musique supprimée avec succès.' });
        return response.redirect('/admin/music');
    }
}
//# sourceMappingURL=music_controller.js.map