"use strict";
/**
 * LangGraph 书籍生成 - 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 book_langgraph_1 = require("./book-langgraph");
const book_generator_store_1 = require("../book-generator.store");
const router = new router_1.default();
/**
 * POST /api/book-generator/langgraph/books
 * 使用 LangGraph 创建并生成书籍
 */
router.post('/books', async (ctx) => {
    try {
        const body = ctx.request.body;
        if (!body.title || !body.description) {
            ctx.status = 400;
            ctx.body = { code: 1, message: '书名和描述不能为空' };
            return;
        }
        const bookScale = body.bookScale || '130000';
        // 创建书籍（先设置一个预估章节数，实际数量由AI分析后确定）
        const scaleToChapters = {
            '800': 1,
            '2000': 1,
            '5000': 1,
            '130000': 10,
            '340000': 15,
        };
        const estimatedChapters = scaleToChapters[bookScale] || 10;
        const book = await book_generator_store_1.bookStore.create({
            title: body.title,
            description: body.description,
            totalChapters: estimatedChapters,
        });
        // 启动 LangGraph 生成（异步，不阻塞）
        book_langgraph_1.langGraphGenerator.generate(book.id, body.description, bookScale).catch(err => {
            console.error('[LangGraph] 生成失败:', err);
        });
        ctx.body = {
            code: 0,
            message: 'LangGraph 生成任务已启动',
            data: {
                bookId: book.id,
                taskId: `lg_${book.id}_${Date.now()}`,
                status: 'started',
            },
        };
    }
    catch (error) {
        console.error('启动失败:', error);
        ctx.status = 500;
        ctx.body = {
            code: 1,
            message: error instanceof Error ? error.message : '启动失败',
        };
    }
});
/**
 * POST /api/book-generator/langgraph/books/:id/generate
 * 对已有书籍使用 LangGraph 生成
 */
router.post('/books/:id/generate', async (ctx) => {
    try {
        const bookId = ctx.params.id;
        const book = await book_generator_store_1.bookStore.getById(bookId);
        if (!book) {
            ctx.status = 404;
            ctx.body = { code: 1, message: '书籍不存在' };
            return;
        }
        // 启动 LangGraph 生成
        book_langgraph_1.langGraphGenerator.generate(bookId, book.description, '130000'
        ).catch(err => {
            console.error('[LangGraph] 生成失败:', err);
        });
        ctx.body = {
            code: 0,
            message: 'LangGraph 生成任务已启动',
            data: {
                bookId,
                taskId: `lg_${bookId}_${Date.now()}`,
                status: 'started',
            },
        };
    }
    catch (error) {
        console.error('启动失败:', error);
        ctx.status = 500;
        ctx.body = {
            code: 1,
            message: error instanceof Error ? error.message : '启动失败',
        };
    }
});
exports.default = router;
