"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const koa_1 = __importDefault(require("koa"));
const cors_1 = __importDefault(require("@koa/cors"));
const router_1 = __importDefault(require("@koa/router"));
const koa_body_1 = __importDefault(require("koa-body"));
const koa_static_1 = __importDefault(require("koa-static"));
const koa_mount_1 = __importDefault(require("koa-mount"));
const path_1 = __importDefault(require("path"));
const config_1 = require("./config");
const models_1 = require("./models");
const errorHandler_1 = require("./middleware/errorHandler");
const auth_controller_1 = __importDefault(require("./modules/auth/auth.controller"));
const tts_controller_1 = __importDefault(require("./modules/tts/tts.controller"));
const member_controller_1 = __importDefault(require("./modules/member/member.controller"));
const share_controller_1 = __importDefault(require("./modules/share/share.controller"));
const ai_controller_1 = __importDefault(require("./modules/ai/ai.controller"));
const ai_content_controller_1 = __importDefault(require("./modules/ai-content/ai-content.controller"));
const player_controller_1 = __importDefault(require("./modules/player/player.controller"));
const favorites_controller_1 = __importDefault(require("./modules/favorites/favorites.controller"));
const preferences_controller_1 = __importDefault(require("./modules/preferences/preferences.controller"));
const search_controller_1 = __importDefault(require("./modules/search/search.controller"));
const categories_controller_1 = __importDefault(require("./modules/categories/categories.controller"));
const comments_controller_1 = __importDefault(require("./modules/comments/comments.controller"));
const notifications_controller_1 = __importDefault(require("./modules/notifications/notifications.controller"));
const templates_controller_1 = __importDefault(require("./modules/templates/templates.controller"));
const bgm_controller_1 = __importDefault(require("./modules/bgm/bgm.controller"));
const audioedit_controller_1 = __importDefault(require("./modules/audioedit/audioedit.controller"));
const book_generator_controller_1 = __importDefault(require("./modules/book-generator/book-generator.controller"));
const controller_1 = __importDefault(require("./modules/book-generator/langgraph/controller"));
const video_generator_controller_1 = __importDefault(require("./modules/video-generator/video-generator.controller"));
const queue_controller_1 = __importDefault(require("./modules/queue/queue.controller"));
const app = new koa_1.default();
const router = new router_1.default();
// 中间件
app.use(errorHandler_1.errorHandler);
app.use((0, cors_1.default)({
    origin: '*',
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    allowHeaders: ['Content-Type', 'Authorization'],
}));
app.use((0, koa_body_1.default)({
    multipart: true,
    formidable: {
        uploadDir: path_1.default.join(process.cwd(), 'uploads', 'materials'),
        keepExtensions: true,
        maxFileSize: 100 * 1024 * 1024, // 100MB
    },
}));
// 静态文件服务 - 音频文件
app.use((0, koa_mount_1.default)('/uploads', (0, koa_static_1.default)(config_1.config.upload.dir)));
// 静态文件服务 - 视频文件
app.use((0, koa_mount_1.default)('/videos', (0, koa_static_1.default)(path_1.default.join(process.cwd(), 'public', 'videos'))));
// 静态文件服务 - 管理后台页面
app.use((0, koa_mount_1.default)('/admin', (0, koa_static_1.default)(path_1.default.join(process.cwd(), 'public', 'admin'))));
// 健康检查
router.get('/health', (ctx) => {
    ctx.body = { code: 0, message: 'ok', data: { status: 'healthy' } };
});
// 注册路由
router.use('/api/auth', auth_controller_1.default.routes());
router.use('/api/tts', tts_controller_1.default.routes());
router.use('/api/member', member_controller_1.default.routes());
router.use('/api/share', share_controller_1.default.routes());
router.use('/api/ai', ai_controller_1.default.routes());
router.use('/api/ai-content', ai_content_controller_1.default.routes());
router.use('/api/player', player_controller_1.default.routes());
router.use('/api/favorites', favorites_controller_1.default.routes());
router.use('/api/user/preferences', preferences_controller_1.default.routes());
router.use('/api/search', search_controller_1.default.routes());
router.use('/api/categories', categories_controller_1.default.routes());
router.use('/api/comments', comments_controller_1.default.routes());
router.use('/api/notifications', notifications_controller_1.default.routes());
router.use('/api/templates', templates_controller_1.default.routes());
router.use('/api/bgm', bgm_controller_1.default.routes());
router.use('/api/audio', audioedit_controller_1.default.routes());
router.use('/api/book-generator', book_generator_controller_1.default.routes());
router.use('/api/book-generator/langgraph', controller_1.default.routes());
router.use('/api/video', video_generator_controller_1.default.routes());
router.use('/api/queue', queue_controller_1.default.routes());
app.use(router.routes()).use(router.allowedMethods());
// 启动服务
async function start() {
    try {
        await (0, models_1.connectDatabase)();
        console.log('✅ MySQL 连接成功');
        app.listen(config_1.config.port, () => {
            console.log(`🚀 服务启动成功: http://localhost:${config_1.config.port}`);
            console.log(`📁 上传目录: ${config_1.config.upload.dir}`);
        });
    }
    catch (error) {
        console.error('❌ 服务启动失败:', error);
        process.exit(1);
    }
}
start();
