"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const router_1 = __importDefault(require("@koa/router"));
const bgm_service_1 = require("./bgm.service");
const router = new router_1.default();
/**
 * 获取背景音乐列表
 * GET /api/bgm
 */
router.get('/', async (ctx) => {
    try {
        const { mood } = ctx.query;
        let bgmList;
        if (mood) {
            bgmList = bgm_service_1.bgmService.getBgmByMood(mood);
        }
        else {
            bgmList = bgm_service_1.bgmService.getBgmList();
        }
        ctx.body = {
            code: 0,
            message: 'success',
            data: bgmList,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取背景音乐列表失败',
        };
    }
});
/**
 * 获取所有风格
 * GET /api/bgm/moods
 */
router.get('/moods', async (ctx) => {
    try {
        const moods = bgm_service_1.bgmService.getMoods();
        ctx.body = {
            code: 0,
            message: 'success',
            data: moods,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取风格列表失败',
        };
    }
});
/**
 * 获取单个背景音乐
 * GET /api/bgm/:id
 */
router.get('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const bgm = bgm_service_1.bgmService.getBgmById(parseInt(id));
        if (!bgm) {
            ctx.body = {
                code: 404,
                message: '背景音乐不存在',
            };
            return;
        }
        ctx.body = {
            code: 0,
            message: 'success',
            data: bgm,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取背景音乐失败',
        };
    }
});
/**
 * 添加背景音乐
 * POST /api/bgm
 */
router.post('/', async (ctx) => {
    try {
        const { name, url, mood, duration } = ctx.request.body;
        if (!name || !url || !mood || !duration) {
            ctx.body = {
                code: 400,
                message: '缺少必填字段',
            };
            return;
        }
        const bgm = bgm_service_1.bgmService.addBgm({ name, url, mood, duration });
        ctx.body = {
            code: 0,
            message: 'success',
            data: bgm,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '添加背景音乐失败',
        };
    }
});
/**
 * 更新背景音乐
 * PUT /api/bgm/:id
 */
router.put('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const { name, url, mood, duration, size } = ctx.request.body;
        const bgm = bgm_service_1.bgmService.updateBgm(parseInt(id), { name, url, mood, duration, size });
        if (!bgm) {
            ctx.body = {
                code: 404,
                message: '背景音乐不存在',
            };
            return;
        }
        ctx.body = {
            code: 0,
            message: 'success',
            data: bgm,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '更新背景音乐失败',
        };
    }
});
/**
 * 删除背景音乐
 * DELETE /api/bgm/:id
 */
router.delete('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const success = bgm_service_1.bgmService.deleteBgm(parseInt(id));
        if (!success) {
            ctx.body = {
                code: 404,
                message: '背景音乐不存在',
            };
            return;
        }
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '删除背景音乐失败',
        };
    }
});
exports.default = router;
