const Sequelize = require('sequelize');

/**
 * 任务状态表模型
 * 记录每个设备的任务执行状态和历史
 */
module.exports = (db) => {
  const task_status= db.define("task_status", {
    // 框架自动创建自增id字段作为主键
    // 任务基本信息
    sn_code: {
      comment: '设备SN码',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: ''
    },
    taskType: {
      comment: '任务类型: get_login_qr_code-登录检查, get_resume-获取简历, search_jobs-搜索岗位, get_job_list-获取岗位列表, auto_deliver-自动投递, chat-聊天, apply-投递',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: ''
    },
    taskName: {
      comment: '任务名称',
      type: Sequelize.STRING(200),
      allowNull: true,
      defaultValue: ''
    },
    // 任务状态
    status: {
      comment: '任务状态: pending-待执行, running-执行中, completed-已完成, failed-失败, timeout-超时, cancelled-已取消',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'pending'
    },
    progress: {
      comment: '任务进度（0-100）',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    currentStep: {
      comment: '当前执行步骤',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    totalSteps: {
      comment: '总步骤数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    // 任务参数
    taskParams: {
      comment: '任务参数(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    // 执行结果
    result: {
      comment: '执行结果(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    errorMessage: {
      comment: '错误信息',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    errorStack: {
      comment: '错误堆栈',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    // 重试信息
    retryCount: {
      comment: '重试次数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    maxRetries: {
      comment: '最大重试次数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 3
    },
    startTime: {
      comment: '开始执行时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    endTime: {
      comment: '结束时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    duration: {
      comment: '执行时长（毫秒）',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    // 统计信息
    jobsSearched: {
      comment: '搜索的岗位数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    jobsFiltered: {
      comment: '筛选后的岗位数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    chatsProcessed: {
      comment: '处理的聊天数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    appliesSubmitted: {
      comment: '投递的简历数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    // 其他信息
    platform: {
      comment: '平台: boss-Boss直聘, liepin-猎聘',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'boss'
    },
    keyword: {
      comment: '搜索关键词',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    priority: {
      comment: '任务优先级（1-10，数字越大优先级越高）',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 5
    },
    scheduledTime: {
      comment: '计划执行时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    notes: {
      comment: '备注',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    }
  }, {
    timestamps: false,
    indexes: [
      {
        unique: false,
        fields: ['sn_code']
      },
      {
        unique: false,
        fields: ['status']
      },
      {
        unique: false,
        fields: ['taskType']
      },
      {
        unique: false,
        fields: ['priority']
      }
    ]
  });

  // task_status.sync({ force: true });

  return task_status

};
