const Sequelize = require('sequelize');

/**
 * 设备状态表模型
 * 记录pydp客户端设备的实时状态和运行信息
 */
module.exports = (db) => {
  const device_status= db.define("device_status", {
    // 设备基本信息
    sn_code: {
      comment: '设备SN码(唯一标识)',
      type: Sequelize.STRING(50),
      allowNull: false,
      primaryKey: true
    },
    device_id:{
      comment: '设备ID',
      type: Sequelize.STRING(200),
      allowNull: false,
      primaryKey: true
    },

    deviceName: {
      comment: '设备名称',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    deviceType: {
      comment: '设备类型',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: 'pydp'
    },
    
    // 在线状态
    isOnline: {
      comment: '是否在线',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    isRunning: {
      comment: '是否运行中',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    lastOnlineTime: {
      comment: '最后在线时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    lastOfflineTime: {
      comment: '最后离线时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    onlineDuration: {
      comment: '在线时长(分钟)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 运行状态
    currentTask: {
      comment: '当前执行任务',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    currentTaskId: {
      comment: '当前任务ID',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    taskStatus: {
      comment: '任务状态',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'idle'
    },
    lastTaskTime: {
      comment: '最后任务时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    
    // 平台信息
    platform: {
      comment: '当前使用平台: boss-Boss直聘, liepin-猎聘',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'boss'
    },
    isLoggedIn: {
      comment: '是否已登录平台',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    loginTime: {
      comment: '登录时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    accountName: {
      comment: '账号名称',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 性能信息
    cpuUsage: {
      comment: 'CPU使用率(%)',
      type: Sequelize.FLOAT,
      allowNull: true,
      defaultValue: 0
    },
    memoryUsage: {
      comment: '内存使用率(%)',
      type: Sequelize.FLOAT,
      allowNull: true,
      defaultValue: 0
    },
    diskUsage: {
      comment: '磁盘使用率(%)',
      type: Sequelize.FLOAT,
      allowNull: true,
      defaultValue: 0
    },
    
    // 网络信息
    ipAddress: {
      comment: 'IP地址',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    macAddress: {
      comment: 'MAC地址',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    networkStatus: {
      comment: '网络状态: good-良好, slow-缓慢, offline-离线',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'good'
    },
    
    // 心跳信息
    lastHeartbeatTime: {
      comment: '最后心跳时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    heartbeatInterval: {
      comment: '心跳间隔(秒)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 30
    },
    missedHeartbeats: {
      comment: '丢失的心跳次数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 版本信息
    clientVersion: {
      comment: '客户端版本',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: ''
    },
    pythonVersion: {
      comment: 'Python版本',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: ''
    },
    osVersion: {
      comment: '操作系统版本',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 统计信息
    totalTasksCompleted: {
      comment: '完成任务总数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    totalTasksFailed: {
      comment: '失败任务总数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    totalJobsSearched: {
      comment: '搜索岗位总数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    totalApplies: {
      comment: '投递简历总数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    totalChats: {
      comment: '聊天总数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 错误信息
    lastError: {
      comment: '最后错误信息',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    lastErrorTime: {
      comment: '最后错误时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    errorCount: {
      comment: '错误次数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 健康状态
    healthStatus: {
      comment: '健康状态: healthy-健康, warning-警告, error-错误, unknown-未知',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'unknown'
    },
    healthScore: {
      comment: '健康评分(0-100)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 配置信息
    config: {
      comment: '设备配置(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // 其他信息
    notes: {
      comment: '备注',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
  }, {
    timestamps: false,
    indexes: [
      {
        unique: false,
        fields: ['isOnline']
      },
      {
        unique: false,
        fields: ['isRunning']
      },
      {
        unique: false,
        fields: ['healthStatus']
      },
      {
        unique: false,
        fields: ['platform']
      },
      {
        unique: false,
        fields: ['lastHeartbeatTime']
      },
    
    ]
  });

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

  return device_status

  // device_status.sync({ force: true 
};

