const dayjs = require("dayjs");
const Sequelize = require('sequelize');

module.exports = (db) => {
    const task_commands = db.define("task_commands", {
        task_id: {
            comment: '任务ID',
            type: Sequelize.INTEGER,
            allowNull: true,
            defaultValue: null
            // 不添加外键关联，避免数据库层面的约束
        },
        command_type: {
            comment: '指令类型',
            type: Sequelize.STRING(50),
            allowNull: false,
            defaultValue: ''
        },
        command_name: {
            comment: '指令名称',
            type: Sequelize.STRING(100),
            allowNull: false,
            defaultValue: ''
        },
        command_params: {
            comment: '指令参数',
            type: Sequelize.TEXT,
            allowNull: true,
            defaultValue: null
        },
        status: {
            comment: '指令状态',
            type: Sequelize.ENUM('pending', 'running', 'completed', 'failed', 'cancelled'),
            allowNull: false,
            defaultValue: 'pending'
        },
        priority: {
            comment: '优先级',
            type: Sequelize.INTEGER,
            allowNull: false,
            defaultValue: 1
        },
        sequence: {
            comment: '执行顺序',
            type: Sequelize.INTEGER,
            allowNull: false,
            defaultValue: 1
        },
        retry_count: {
            comment: '重试次数',
            type: Sequelize.INTEGER,
            allowNull: false,
            defaultValue: 0
        },
        max_retries: {
            comment: '最大重试次数',
            type: Sequelize.INTEGER,
            allowNull: false,
            defaultValue: 3
        },
        start_time: {
            comment: '开始时间',
            type: Sequelize.DATE,
            allowNull: true,
            defaultValue: null
        },
        end_time: {
            comment: '结束时间',
            type: Sequelize.DATE,
            allowNull: true,
            defaultValue: null
        },
        duration: {
            comment: '执行时长(毫秒)',
            type: Sequelize.INTEGER,
            allowNull: true,
            defaultValue: null
        },
        result: {
            comment: '执行结果',
            type: Sequelize.TEXT,
            allowNull: true,
            defaultValue: null
        },
        error_message: {
            comment: '错误信息',
            type: Sequelize.TEXT,
            allowNull: true,
            defaultValue: null
        },
        error_stack: {
            comment: '错误堆栈',
            type: Sequelize.TEXT,
            allowNull: true,
            defaultValue: null
        },
        progress: {
            comment: '执行进度(0-100)',
            type: Sequelize.INTEGER,
            allowNull: false,
            defaultValue: 0
        },
    }, {
        tableName: 'task_commands',
        indexes: [
            {
                fields: ['task_id']
            },
            {
                fields: ['status']
            },
            {
                fields: ['command_type']
            },
            {
                fields: ['priority', 'sequence']
            },
           
        ]
    });

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

    return task_commands;
};
