"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 share_service_1 = require("./share.service");
const auth_1 = require("../../middleware/auth");
const router = new router_1.default();
/**
 * 获取分享信息（兼容feature_list测试接口）
 * GET /api/share?audioId=xxx
 */
router.get('/', async (ctx) => {
    try {
        const { audioId } = ctx.query;
        if (!audioId) {
            ctx.body = {
                code: 400,
                message: 'audioId is required',
            };
            return;
        }
        const card = await share_service_1.shareService.generateShareCard(audioId);
        ctx.body = {
            code: 0,
            message: 'success',
            data: card,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取分享信息失败',
        };
    }
});
/**
 * 获取分享卡片信息
 * GET /api/share/card/:audioId
 */
router.get('/card/:audioId', async (ctx) => {
    try {
        const { audioId } = ctx.params;
        const card = await share_service_1.shareService.generateShareCard(audioId);
        ctx.body = {
            code: 0,
            message: 'success',
            data: card,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取分享信息失败',
        };
    }
});
/**
 * 生成二维码数据
 * GET /api/share/qrcode/:audioId
 */
router.get('/qrcode/:audioId', async (ctx) => {
    try {
        const { audioId } = ctx.params;
        const qrData = share_service_1.shareService.generateQRCodeData(audioId);
        ctx.body = {
            code: 0,
            message: 'success',
            data: {
                text: qrData,
            },
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '生成二维码失败',
        };
    }
});
/**
 * 记录分享行为
 * POST /api/share/track
 */
router.post('/track', auth_1.authMiddleware, async (ctx) => {
    try {
        const { audioId, platform } = ctx.request.body;
        const userId = ctx.state.user.id;
        await share_service_1.shareService.trackShare(audioId, userId, platform);
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '记录分享失败',
        };
    }
});
exports.default = router;
