"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 MemberService = __importStar(require("./member.service"));
const errorHandler_1 = require("../../middleware/errorHandler");
const auth_1 = require("../../middleware/auth");
const router = new router_1.default();
// 获取会员权益信息
router.get('/benefits', async (ctx) => {
    const benefits = MemberService.getMemberBenefits();
    ctx.body = {
        code: 0,
        message: 'success',
        data: benefits,
    };
});
// 获取用户会员状态
router.get('/status', auth_1.authMiddleware, async (ctx) => {
    const userId = ctx.state.user.userId;
    const status = await MemberService.getMemberStatus(userId);
    ctx.body = {
        code: 0,
        message: 'success',
        data: status,
    };
});
// 创建订单
router.post('/order', auth_1.authMiddleware, async (ctx) => {
    const userId = ctx.state.user.userId;
    const { productType } = ctx.request.body;
    if (!['monthly', 'yearly'].includes(productType)) {
        throw new errorHandler_1.BadRequestError('无效的产品类型');
    }
    const result = await MemberService.createOrder(userId, productType);
    ctx.body = {
        code: 0,
        message: '订单创建成功',
        data: result,
    };
});
// 模拟支付（仅开发环境）
router.post('/pay/mock', auth_1.authMiddleware, async (ctx) => {
    if (process.env.NODE_ENV === 'production') {
        throw new errorHandler_1.BadRequestError('生产环境不可用');
    }
    const userId = ctx.state.user.userId;
    const { orderNo } = ctx.request.body;
    if (!orderNo) {
        throw new errorHandler_1.BadRequestError('订单号不能为空');
    }
    const result = await MemberService.mockPaymentSuccess(orderNo, userId);
    ctx.body = {
        code: 0,
        message: '支付成功',
        data: result,
    };
});
// 获取订单列表
router.get('/orders', auth_1.authMiddleware, async (ctx) => {
    const userId = ctx.state.user.userId;
    const { page = 1, pageSize = 10 } = ctx.query;
    const result = await MemberService.getOrders(userId, Number(page) || 1, Number(pageSize) || 10);
    ctx.body = {
        code: 0,
        message: 'success',
        data: result,
    };
});
exports.default = router;
