/**
 * 书籍生成 - API 路由
 * 包含一键完整生成 API
 */

import Router from '@koa/router';
import { Context } from 'koa';
import { bookStore } from './book-generator.store';
import {
  createBatchGenerationTask,
  setCancellationFlag,
  GenerationStep
} from './book-generator.service';

// 任务存储（用于取消操作）
const runningTasks = new Map<string, { taskId: string; bookId: string }>();

const router = new Router();

/**
 * POST /api/book-generator/books/:id/batch-generate
 * 一键完整生成书籍（内容、音频、合并、生成视频、合并视频）
 */
router.post('/books/:id/batch-generate', async (ctx: Context) => {
  try {
    const bookId = ctx.params.id as string;
    const body = ctx.request.body as {
      steps?: GenerationStep[];
    };

    // 验证书籍存在
    const book = await bookStore.getById(bookId);
    if (!book) {
      ctx.status = 404;
      ctx.body = { code: 1, message: '书籍不存在' };
      return;
    }

    // 默认执行全部步骤
    const steps: GenerationStep[] = body.steps || [
      'generate_content',
      'generate_audio',
      'merge_audio',
      'generate_video',
      'merge_video'
    ];

    // 验证步骤
    const validSteps: GenerationStep[] = [
      'generate_content',
      'generate_audio',
      'merge_audio',
      'generate_video',
      'merge_video'
    ];
    for (const step of steps) {
      if (!validSteps.includes(step)) {
        ctx.status = 400;
        ctx.body = { code: 1, message: `无效的步骤: ${step}` };
        return;
      }
    }

    // 检查是否有正在运行的批量生成任务
    const existingTask = runningTasks.get(bookId);
    if (existingTask) {
      ctx.status = 400;
      ctx.body = {
        code: 1,
        message: '书籍已有批量生成任务在运行，请先取消后再试',
        data: { taskId: existingTask.taskId }
      };
      return;
    }

    // 创建批量生成任务
    const { taskId, orchestrator } = await createBatchGenerationTask(bookId, steps);

    // 存储任务信息
    runningTasks.set(bookId, { taskId, bookId });

    // 后台执行任务
    (async () => {
      try {
        const result = await orchestrator.execute();
        console.log(`[BatchGen][${taskId}] 任务完成:`, result);

        // 发送完成消息
        const { pushBatchGenerationProgress } = await import('../../services/websocket.service.js');
        pushBatchGenerationProgress(taskId, 'completed', 100);

      } catch (error: any) {
        console.error(`[BatchGen][${taskId}] 任务异常:`, error);
      } finally {
        // 清理任务
        runningTasks.delete(bookId);
      }
    })();

    ctx.body = {
      code: 0,
      message: '批量生成任务已启动',
      data: {
        taskId,
        bookId,
        status: 'started',
        steps
      }
    };

  } catch (error) {
    console.error('[BatchGenerate] 启动失败:', error);
    ctx.status = 500;
    ctx.body = {
      code: 1,
      message: error instanceof Error ? error.message : '启动失败'
    };
  }
});

/**
 * POST /api/book-generator/books/:id/batch-generate/cancel
 * 取消批量生成任务
 */
router.post('/books/:id/batch-generate/cancel', async (ctx: Context) => {
  try {
    const bookId = ctx.params.id as string;

    const task = runningTasks.get(bookId);
    if (!task) {
      ctx.status = 404;
      ctx.body = { code: 1, message: '没有正在运行的批量生成任务' };
      return;
    }

    // 设置取消标志
    setCancellationFlag(task.taskId);

    ctx.body = {
      code: 0,
      message: '取消指令已发送',
      data: {
        taskId: task.taskId,
        bookId
      }
    };

  } catch (error) {
    console.error('[BatchGenerate] 取消失败:', error);
    ctx.status = 500;
    ctx.body = {
      code: 1,
      message: error instanceof Error ? error.message : '取消失败'
    };
  }
});

/**
 * GET /api/book-generator/books/:id/batch-generate/status
 * 获取批量生成任务状态
 */
router.get('/books/:id/batch-generate/status', async (ctx: Context) => {
  try {
    const bookId = ctx.params.id as string;

    const task = runningTasks.get(bookId);
    if (!task) {
      ctx.body = {
        code: 0,
        message: 'success',
        data: {
          isRunning: false,
          bookId
        }
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: {
        isRunning: true,
        taskId: task.taskId,
        bookId: task.bookId
      }
    };

  } catch (error) {
    console.error('[BatchGenerate] 查询状态失败:', error);
    ctx.status = 500;
    ctx.body = {
      code: 1,
      message: error instanceof Error ? error.message : '查询失败'
    };
  }
});

export default router;