const Sequelize = require('sequelize');

/**
 * 岗位信息表模型
 * 存储从招聘平台获取的岗位信息及AI分析结果
 */
module.exports = (db) => {
  const job_postings = db.define("job_postings", {
    sn_code: {
      comment: '设备SN码',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    // 岗位基础信息
    encryptBossId: {
      comment: 'Boss加密ID',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    securityId: {
      comment: '安全ID',
      type: Sequelize.STRING(255),
      allowNull: true,
      defaultValue: ''
    },
    jobId: {
      comment: '岗位ID',
      type: Sequelize.STRING(100),
      allowNull: false,
      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: ''
    },
    companySize: {
      comment: '公司规模',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    companyIndustry: {
      comment: '公司行业',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    salary: {
      comment: '薪资范围',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    jobRequirements: {
      comment: '岗位要求',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    jobDescription: {
      comment: '岗位描述',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    location: {
      comment: '工作地点',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },

    // 经度
    longitude: {
      comment: '经度',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    // 纬度
    latitude: {
      comment: '纬度',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },

    experience: {
      comment: '工作经验要求',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    education: {
      comment: '学历要求',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    // AI分析结果
    aiMatchScore: {
      comment: 'AI匹配度评分',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    aiSkillMatch: {
      comment: 'AI技能匹配度',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    aiExperienceMatch: {
      comment: 'AI经验匹配度',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    aiSalaryReasonable: {
      comment: 'AI薪资合理性评分',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    aiCompanyQuality: {
      comment: 'AI公司质量评分',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    isOutsourcing: {
      comment: '是否外包岗位',
      type: Sequelize.BOOLEAN,
      allowNull: true,
      defaultValue: false
    },
    aiAnalysis: {
      comment: 'AI详细分析结果(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    outsourcingAnalysis: {
      comment: '外包识别分析结果',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    // 投递状态
    applyStatus: {
      comment: '投递状态: pending-待投递, applied-已投递, rejected-被拒绝, accepted-已接受',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'pending'
    },
    applyTime: {
      comment: '投递时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    chatStatus: {
      comment: '聊天状态: none-未聊天, sent-已发送, replied-已回复',
      type: Sequelize.STRING(20),
      allowNull: true,
      defaultValue: 'none'
    },
    chatTime: {
      comment: '聊天时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    // 关联信息
    platform: {
      comment: '平台: boss-Boss直聘, liepin-猎聘',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'boss'
    },
 
    keyword: {
      comment: '搜索关键词',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    // 其他信息
    originalData: {
      comment: '原始数据(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    }
  }, {
    timestamps: true,
    createdAt: 'create_time',
    updatedAt: 'last_modify_time',
    indexes: [
      {
        unique: false,
        fields: ['jobId']
      },
      {
        unique: false,
        fields: ['encryptBossId']
      },
      {
        unique: false,
        fields: ['sn_code']
      },
      {
        unique: false,
        fields: ['applyStatus']
      },
      {
        unique: false,
        fields: ['aiMatchScore']
      }
    ]
  });

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

  return job_postings

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