"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 albums_service_1 = require("./albums.service");
const router = new router_1.default();
/**
 * 获取或创建默认专辑
 * GET /api/albums/default
 */
router.get('/default', async (ctx) => {
    try {
        const defaultAlbum = await albums_service_1.albumsService.getOrCreateDefaultAlbum();
        ctx.body = {
            code: 0,
            message: 'success',
            data: defaultAlbum,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取默认专辑失败',
        };
    }
});
/**
 * 创建专辑
 * POST /api/albums
 */
router.post('/', async (ctx) => {
    try {
        const data = ctx.request.body;
        if (!data.name) {
            ctx.body = { code: 400, message: '专辑名称不能为空' };
            return;
        }
        const album = await albums_service_1.albumsService.createAlbum(data);
        ctx.body = {
            code: 0,
            message: 'success',
            data: album,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '创建专辑失败',
        };
    }
});
/**
 * 获取专辑列表
 * GET /api/albums
 */
router.get('/', async (ctx) => {
    try {
        const { page, pageSize } = ctx.query;
        const result = await albums_service_1.albumsService.getAlbums({
            page: page ? parseInt(page) : 1,
            pageSize: pageSize ? parseInt(pageSize) : 20,
        });
        ctx.body = {
            code: 0,
            message: 'success',
            data: result,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取专辑列表失败',
        };
    }
});
/**
 * 获取专辑详情
 * GET /api/albums/:id
 */
router.get('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const album = await albums_service_1.albumsService.getAlbumById(parseInt(id));
        ctx.body = {
            code: 0,
            message: 'success',
            data: album,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取专辑详情失败',
        };
    }
});
/**
 * 更新专辑
 * PUT /api/albums/:id
 */
router.put('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const data = ctx.request.body;
        const album = await albums_service_1.albumsService.updateAlbum(parseInt(id), data);
        ctx.body = {
            code: 0,
            message: 'success',
            data: album,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '更新专辑失败',
        };
    }
});
/**
 * 删除专辑
 * DELETE /api/albums/:id
 */
router.delete('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        await albums_service_1.albumsService.deleteAlbum(parseInt(id));
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '删除专辑失败',
        };
    }
});
/**
 * 获取专辑内的音频列表
 * GET /api/albums/:id/audios
 */
router.get('/:id/audios', async (ctx) => {
    try {
        const { id } = ctx.params;
        const audios = await albums_service_1.albumsService.getAlbumAudios(parseInt(id));
        ctx.body = {
            code: 0,
            message: 'success',
            data: audios,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取专辑音频列表失败',
        };
    }
});
/**
 * 添加音频到专辑
 * POST /api/albums/:id/audio
 */
router.post('/:id/audio', async (ctx) => {
    try {
        const { id } = ctx.params;
        const { audioId, order } = ctx.request.body;
        if (!audioId) {
            ctx.body = { code: 400, message: 'audioId 不能为空' };
            return;
        }
        const albumAudio = await albums_service_1.albumsService.addAudioToAlbum(parseInt(id), audioId, order || 0);
        ctx.body = {
            code: 0,
            message: 'success',
            data: albumAudio,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '添加音频到专辑失败',
        };
    }
});
/**
 * 从专辑移除音频
 * DELETE /api/albums/:id/audio/:audioId
 */
router.delete('/:id/audio/:audioId', async (ctx) => {
    try {
        const { id, audioId } = ctx.params;
        await albums_service_1.albumsService.removeAudioFromAlbum(parseInt(id), parseInt(audioId));
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '从专辑移除音频失败',
        };
    }
});
/**
 * 调整专辑中音频的顺序
 * PUT /api/albums/:id/audio/:audioId
 */
router.put('/:id/audio/:audioId', async (ctx) => {
    try {
        const { id, audioId } = ctx.params;
        const { order } = ctx.request.body;
        if (order === undefined) {
            ctx.body = { code: 400, message: 'order 不能为空' };
            return;
        }
        const albumAudio = await albums_service_1.albumsService.reorderAudio(parseInt(id), parseInt(audioId), order);
        ctx.body = {
            code: 0,
            message: 'success',
            data: albumAudio,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '调整音频顺序失败',
        };
    }
});
/**
 * 检查订阅状态
 * GET /api/albums/:id/check-subscribe
 */
router.get('/:id/check-subscribe', async (ctx) => {
    try {
        const { id } = ctx.params;
        const { userId } = ctx.query;
        if (!userId) {
            ctx.body = { code: 400, message: 'userId 不能为空' };
            return;
        }
        const isSubscribed = await albums_service_1.albumsService.isSubscribed(parseInt(userId), parseInt(id));
        ctx.body = {
            code: 0,
            message: 'success',
            data: { isSubscribed },
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '检查订阅状态失败',
        };
    }
});
/**
 * 订阅专辑
 * POST /api/albums/:id/subscribe
 */
router.post('/:id/subscribe', async (ctx) => {
    try {
        const { id } = ctx.params;
        const { userId } = ctx.request.body;
        if (!userId) {
            ctx.body = { code: 400, message: 'userId 不能为空' };
            return;
        }
        await albums_service_1.albumsService.subscribeAlbum(userId, parseInt(id));
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '订阅专辑失败',
        };
    }
});
/**
 * 取消订阅专辑
 * DELETE /api/albums/:id/subscribe
 */
router.delete('/:id/subscribe', async (ctx) => {
    try {
        const { id } = ctx.params;
        const { userId } = ctx.query;
        if (!userId) {
            ctx.body = { code: 400, message: 'userId 不能为空' };
            return;
        }
        await albums_service_1.albumsService.unsubscribeAlbum(parseInt(userId), parseInt(id));
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '取消订阅专辑失败',
        };
    }
});
/**
 * 获取用户已订阅的专辑列表
 * GET /api/albums/subscribed
 */
router.get('/subscribed/list', async (ctx) => {
    try {
        const { userId } = ctx.query;
        if (!userId) {
            ctx.body = { code: 400, message: 'userId 不能为空' };
            return;
        }
        const albums = await albums_service_1.albumsService.getSubscribedAlbums(parseInt(userId));
        ctx.body = {
            code: 0,
            message: 'success',
            data: albums,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取已订阅专辑列表失败',
        };
    }
});
exports.default = router;
