import { Router } from 'express';
import { body, param } from 'express-validator';
import { PublishService } from '../services/PublishService.js';
import { taskQueueService } from '../services/TaskQueueService.js';
import { authenticate } from '../middleware/auth.js';
import { asyncHandler } from '../middleware/error.js';
import { validateRequest } from '../middleware/validate.js';

const router = Router();
const publishService = new PublishService();

router.use(authenticate);

// 获取发布任务列表
router.get(
  '/',
  asyncHandler(async (req, res) => {
    const { page = 1, pageSize = 20, status } = req.query;
    const result = await publishService.getTasks(req.user!.userId, {
      page: Number(page),
      pageSize: Number(pageSize),
      status: status as string,
    });
    res.json({ success: true, data: result });
  })
);

// 获取任务详情
router.get(
  '/:id',
  [
    param('id').isInt().withMessage('任务ID无效'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    const task = await publishService.getTaskById(
      req.user!.userId,
      Number(req.params.id)
    );
    res.json({ success: true, data: task });
  })
);

// 创建发布任务
router.post(
  '/',
  [
    body('videoPath').notEmpty().withMessage('视频路径不能为空'),
    body('title').notEmpty().withMessage('标题不能为空'),
    body('targetAccounts').isArray({ min: 1 }).withMessage('至少选择一个目标账号'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    const userId = req.user!.userId;
    
    // 1. 创建数据库记录
    const task = await publishService.createTask(userId, req.body);
    
    // 2. 如果不是定时任务，加入任务队列
    if (!req.body.scheduledAt) {
      taskQueueService.createTask(userId, {
        type: 'publish_video',
        title: `发布视频: ${req.body.title}`,
        description: `发布到 ${req.body.targetAccounts.length} 个账号`,
        data: {
          publishTaskId: task.id,
        },
      });
    }
    
    res.status(201).json({ success: true, data: task });
  })
);

// 取消任务
router.post(
  '/:id/cancel',
  [
    param('id').isInt().withMessage('任务ID无效'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    await publishService.cancelTask(req.user!.userId, Number(req.params.id));
    res.json({ success: true, message: '任务已取消' });
  })
);

// 重试任务
router.post(
  '/:id/retry',
  [
    param('id').isInt().withMessage('任务ID无效'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    const userId = req.user!.userId;
    const taskId = Number(req.params.id);
    
    // 1. 更新数据库状态
    const task = await publishService.retryTask(userId, taskId);
    
    // 2. 加入任务队列重新执行
    taskQueueService.createTask(userId, {
      type: 'publish_video',
      title: `重试发布: ${task.title}`,
      description: `重新发布到 ${task.targetAccounts.length} 个账号`,
      data: {
        publishTaskId: task.id,
      },
    });
    
    res.json({ success: true, data: task });
  })
);

// 删除任务
router.delete(
  '/:id',
  [
    param('id').isInt().withMessage('任务ID无效'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    await publishService.deleteTask(req.user!.userId, Number(req.params.id));
    res.json({ success: true, message: '任务已删除' });
  })
);

// 单账号有头浏览器重试发布（用于验证码场景）
router.post(
  '/:taskId/retry-headful/:accountId',
  [
    param('taskId').isInt().withMessage('任务ID无效'),
    param('accountId').isInt().withMessage('账号ID无效'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    const userId = req.user!.userId;
    const taskId = Number(req.params.taskId);
    const accountId = Number(req.params.accountId);
    
    // 调用服务层执行有头浏览器发布
    const result = await publishService.retryAccountWithHeadfulBrowser(
      userId, 
      taskId, 
      accountId
    );
    
    res.json({ success: true, data: result });
  })
);

export default router;
