"use strict";
/**
 * 视频生成模块 - API 路由控制器
 */
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 video_generator_service_1 = require("./video-generator.service");
const router = new router_1.default();
// ============ 视频项目管理 ============
/**
 * GET /api/video/projects
 * 获取视频项目列表
 */
router.get('/projects', async (ctx) => {
    const query = {
        userId: ctx.query.userId ? Number(ctx.query.userId) : undefined,
        status: ctx.query.status,
        page: ctx.query.page ? Number(ctx.query.page) : 1,
        pageSize: ctx.query.pageSize ? Number(ctx.query.pageSize) : 10,
    };
    const result = await (0, video_generator_service_1.getVideoProjects)(query);
    ctx.body = { success: true, data: result };
});
/**
 * POST /api/video/projects
 * 创建视频项目
 */
router.post('/projects', async (ctx) => {
    const body = ctx.request.body;
    const userId = ctx.state.user?.id;
    const project = await (0, video_generator_service_1.createVideoProject)(body, userId);
    ctx.body = { success: true, data: project };
});
/**
 * GET /api/video/projects/:id
 * 获取视频项目详情
 */
router.get('/projects/:id', async (ctx) => {
    const id = Number(ctx.params.id);
    const project = await (0, video_generator_service_1.getVideoProject)(id);
    if (!project) {
        ctx.status = 404;
        ctx.body = { success: false, error: '项目不存在' };
        return;
    }
    ctx.body = { success: true, data: project };
});
/**
 * PUT /api/video/projects/:id
 * 更新视频项目
 */
router.put('/projects/:id', async (ctx) => {
    const id = Number(ctx.params.id);
    const body = ctx.request.body;
    const project = await (0, video_generator_service_1.updateVideoProject)(id, body);
    if (!project) {
        ctx.status = 404;
        ctx.body = { success: false, error: '项目不存在' };
        return;
    }
    ctx.body = { success: true, data: project };
});
/**
 * DELETE /api/video/projects/:id
 * 删除视频项目
 */
router.delete('/projects/:id', async (ctx) => {
    const id = Number(ctx.params.id);
    const success = await (0, video_generator_service_1.deleteVideoProject)(id);
    if (!success) {
        ctx.status = 404;
        ctx.body = { success: false, error: '项目不存在' };
        return;
    }
    ctx.body = { success: true };
});
// ============ 视频生成 ============
/**
 * POST /api/video/projects/:id/generate
 * 开始生成视频
 */
router.post('/projects/:id/generate', async (ctx) => {
    const id = Number(ctx.params.id);
    const result = await (0, video_generator_service_1.generateVideoForProject)(id);
    if (!result.success) {
        ctx.status = 400;
        ctx.body = { success: false, error: result.error };
        return;
    }
    ctx.body = {
        success: true,
        data: {
            outputUrl: result.outputUrl,
            duration: result.duration,
            fileSize: result.fileSize,
        },
    };
});
/**
 * GET /api/video/projects/:id/status
 * 获取生成状态
 */
router.get('/projects/:id/status', async (ctx) => {
    const id = Number(ctx.params.id);
    const status = await (0, video_generator_service_1.getGenerateProgress)(id);
    ctx.body = { success: true, data: status };
});
// ============ 素材管理 ============
/**
 * GET /api/video/materials
 * 获取素材列表
 */
router.get('/materials', async (ctx) => {
    const query = {
        userId: ctx.query.userId ? Number(ctx.query.userId) : undefined,
        type: ctx.query.type,
        category: ctx.query.category,
        page: ctx.query.page ? Number(ctx.query.page) : 1,
        pageSize: ctx.query.pageSize ? Number(ctx.query.pageSize) : 20,
    };
    const result = await (0, video_generator_service_1.getMaterials)(query);
    ctx.body = { success: true, data: result };
});
/**
 * POST /api/video/materials/upload
 * 上传素材（处理 multipart/form-data 文件上传）
 */
router.post('/materials/upload', async (ctx) => {
    const userId = ctx.state.user?.id;
    // 处理 multipart form data
    const body = ctx.request.body;
    const files = ctx.request.files;
    // 获取上传的文件
    const file = files?.file;
    let materialUrl = '';
    if (file) {
        // 文件已上传，url 就是文件的路径
        materialUrl = `/uploads/materials/${file.newFilename || file.filename}`;
    }
    else if (body.url) {
        // 如果没有文件，使用传入的url
        materialUrl = body.url;
    }
    else {
        ctx.status = 400;
        ctx.body = { success: false, error: 'No file uploaded' };
        return;
    }
    const material = await (0, video_generator_service_1.uploadMaterial)({
        type: body.type || 'image',
        name: body.name || file?.newFilename || 'unnamed',
        url: materialUrl,
        thumbnail: body.thumbnail,
        tags: body.tags ? JSON.parse(body.tags) : [],
        category: body.category,
        duration: body.duration ? Number(body.duration) : undefined,
        size: body.size ? Number(body.size) : undefined,
        width: body.width ? Number(body.width) : undefined,
        height: body.height ? Number(body.height) : undefined,
    }, userId);
    ctx.body = { success: true, data: material };
});
/**
 * DELETE /api/video/materials/:id
 * 删除素材
 */
router.delete('/materials/:id', async (ctx) => {
    const id = Number(ctx.params.id);
    const success = await (0, video_generator_service_1.deleteMaterial)(id);
    if (!success) {
        ctx.status = 404;
        ctx.body = { success: false, error: '素材不存在' };
        return;
    }
    ctx.body = { success: true };
});
// ============ 快捷入口 ============
/**
 * POST /api/video/books/:bookId/generate
 * 从书籍生成视频项目
 */
router.post('/books/:bookId/generate', async (ctx) => {
    const bookId = Number(ctx.params.bookId);
    const userId = ctx.state.user?.id;
    const project = await (0, video_generator_service_1.createVideoProjectFromBook)(bookId, userId);
    if (!project) {
        ctx.status = 404;
        ctx.body = { success: false, error: '书籍不存在' };
        return;
    }
    ctx.body = { success: true, data: project };
});
exports.default = router;
