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

/**
 * 投递记录表模型
 * 记录简历投递的详细信息和结果
 */
module.exports = (db) => {
  const apply_records =   db.define('apply_records', {
    // 投递基本信息
    sn_code: {
      comment: '设备SN码',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: ''
    },
    platform: {
      comment: '平台: boss-Boss直聘, liepin-猎聘',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'boss'
    },
    
    // 岗位信息
    jobId: {
      comment: '岗位ID',
      type: Sequelize.STRING(100),
      allowNull: false,
      defaultValue: ''
    },
    encryptBossId: {
      comment: 'Boss加密ID',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    jobTitle: {
      comment: '岗位名称',
      type: Sequelize.STRING(200),
      allowNull: false,
      defaultValue: ''
    },
    companyId: {
      comment: '公司ID',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    companyName: {
      comment: '公司名称',
      type: Sequelize.STRING(200),
      allowNull: false,
      defaultValue: ''
    },
    salary: {
      comment: '薪资范围',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    location: {
      comment: '工作地点',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 简历信息
    resumeId: {
      comment: '简历ID',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    resumeName: {
      comment: '简历名称',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 投递状态
    applyStatus: {
      comment: '投递状态: pending-待投递, applying-投递中, success-投递成功, failed-投递失败, duplicate-重复投递',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'pending'
    },
    applyTime: {
      comment: '投递时间',
      type: Sequelize.DATE,
      allowNull: true,
      get: function() {
        return  dayjs(this.getDataValue('applyTime')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    
    // 反馈状态
    feedbackStatus: {
      comment: '反馈状态: none-无反馈, viewed-已查看, interested-感兴趣, not_suitable-不合适, interview-面试邀约',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'none'
    },
    feedbackTime: {
      comment: '反馈时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    feedbackContent: {
      comment: '反馈内容',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // HR信息
    hrName: {
      comment: 'HR姓名',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    hrTitle: {
      comment: 'HR职位',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    hrId: {
      comment: 'HR ID',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 查看信息
    isViewed: {
      comment: '是否被查看',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    viewTime: {
      comment: '查看时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    viewCount: {
      comment: '查看次数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 沟通信息
    hasChatted: {
      comment: '是否已沟通',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    firstChatTime: {
      comment: '首次沟通时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    lastChatTime: {
      comment: '最后沟通时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    chatCount: {
      comment: '沟通次数',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 面试信息
    hasInterview: {
      comment: '是否有面试',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    interviewTime: {
      comment: '面试时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    interviewLocation: {
      comment: '面试地点',
      type: Sequelize.STRING(200),
      allowNull: true,
      defaultValue: ''
    },
    interviewType: {
      comment: '面试类型: online-线上, offline-线下',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: ''
    },
    interviewStatus: {
      comment: '面试状态: pending-待面试, completed-已完成, passed-通过, failed-未通过, cancelled-已取消',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: ''
    },
    
    // Offer信息
    hasOffer: {
      comment: '是否收到Offer',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    offerTime: {
      comment: 'Offer时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    offerSalary: {
      comment: 'Offer薪资',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    offerStatus: {
      comment: 'Offer状态: pending-待确认, accepted-已接受, rejected-已拒绝',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: ''
    },
    
    // AI匹配信息
    matchScore: {
      comment: 'AI匹配度评分(0-100)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    isOutsourcing: {
      comment: '是否外包岗位',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    
    // 投递策略
    priority: {
      comment: '投递优先级(1-10)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 5
    },
    isAutoApply: {
      comment: '是否自动投递',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: true
    },
    
    // 关联信息
    taskId: {
      comment: '关联任务ID',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    keyword: {
      comment: '搜索关键词',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    
    // 统计信息
    responseDuration: {
      comment: '响应时长(小时)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 其他信息
    originalData: {
      comment: '原始数据(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    errorMessage: {
      comment: '错误信息',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    notes: {
      comment: '备注',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    }
  }, {
    timestamps: false,
    indexes: [
      {
        unique: false,
        fields: ['sn_code']
      },
      {
        unique: false,
        fields: ['jobId']
      },
      {
        unique: false,
        fields: ['encryptBossId']
      },
      {
        unique: false,
        fields: ['applyStatus']
      },
      {
        unique: false,
        fields: ['feedbackStatus']
      },
      {
        unique: false,
        fields: ['isViewed']
      },
      {
        unique: false,
        fields: ['hasChatted']
      },
      {
        unique: false,
        fields: ['hasInterview']
      },
      {
        unique: false,
        fields: ['hasOffer']
      },
      {
        unique: false,
        fields: ['applyTime']
      },
    ]
  });

//  apply_records.sync({ force: true });
  return    apply_records
};

