"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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 AudioService = __importStar(require("./audio.service"));
const errorHandler_1 = require("../../middleware/errorHandler");
const auth_1 = require("../../middleware/auth");
const router = new router_1.default();
// 获取音频列表
router.get('/list', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const { page = 1, pageSize = 10, isFavorite, keyword, category } = ctx.query;
    const result = await AudioService.getAudioList(userId || '', {
        page: Number(page) || 1,
        pageSize: Number(pageSize) || 10,
        isFavorite: isFavorite === 'true' ? true : isFavorite === 'false' ? false : undefined,
        keyword,
        category,
    });
    ctx.body = {
        code: 0,
        message: 'success',
        data: result,
    };
});
// 获取收藏列表
router.get('/favorites', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const { page = 1, pageSize = 20 } = ctx.query;
    const result = await AudioService.getAudioList(userId || '', {
        page: Number(page) || 1,
        pageSize: Number(pageSize) || 20,
        isFavorite: true,
    });
    ctx.body = {
        code: 0,
        message: 'success',
        data: result,
    };
});
// 获取单个音频详情
router.get('/:id', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const { id } = ctx.params;
    const audio = await AudioService.getAudioById(id, userId);
    if (!audio) {
        throw new errorHandler_1.NotFoundError('音频不存在');
    }
    ctx.body = {
        code: 0,
        message: 'success',
        data: audio,
    };
});
// 删除音频
router.delete('/:id', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const { id } = ctx.params;
    const success = await AudioService.deleteAudio(id, userId);
    if (!success) {
        throw new errorHandler_1.NotFoundError('音频不存在或无权删除');
    }
    ctx.body = {
        code: 0,
        message: '删除成功',
        data: null,
    };
});
// 切换收藏状态
router.put('/:id/favorite', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const { id } = ctx.params;
    const audio = await AudioService.toggleFavorite(id, userId);
    if (!audio) {
        throw new errorHandler_1.NotFoundError('音频不存在');
    }
    ctx.body = {
        code: 0,
        message: 'success',
        data: { isFavorite: audio.isFavorite },
    };
});
// 更新音频信息
router.put('/:id', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const { id } = ctx.params;
    const { title, tags } = ctx.request.body;
    const audio = await AudioService.updateAudio(id, userId, { title, tags });
    if (!audio) {
        throw new errorHandler_1.NotFoundError('音频不存在');
    }
    ctx.body = {
        code: 0,
        message: '更新成功',
        data: audio,
    };
});
// 获取用户统计
router.get('/stats/overview', auth_1.optionalAuth, async (ctx) => {
    const userId = ctx.state.user?.userId;
    const stats = await AudioService.getUserStats(userId);
    ctx.body = {
        code: 0,
        message: 'success',
        data: stats,
    };
});
exports.default = router;
