const Sequelize = require('sequelize');

/**
 * AI消息表模型
 * 记录用户与AI的交互消息
 */
module.exports = (db) => {
  const ai_messages = db.define("ai_messages", {
    // 用户信息
    user_id: {
      comment: '用户ID',
      type: Sequelize.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    nickname: {
      comment: '用户昵称',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 消息信息
    message_type: {
      comment: '消息类型: question-提问, feedback-反馈, suggestion-建议',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: 'question'
    },
    content: {
      comment: '消息内容',
      type: Sequelize.TEXT,
      allowNull: false,
      defaultValue: ''
    },
    
    // AI回复信息
    ai_response: {
      comment: 'AI回复内容',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    ai_model: {
      comment: 'AI模型名称',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    ai_prompt: {
      comment: 'AI提示词',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // 处理状态
    status: {
      comment: '处理状态: pending-待处理, processing-处理中, completed-已完成, failed-处理失败',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'pending'
    },
    process_time: {
      comment: '处理时间(毫秒)',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    
    // 关联信息
    related_id: {
      comment: '关联ID（如职位ID、聊天记录ID等）',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    related_type: {
      comment: '关联类型: job-职位, chat-聊天, resume-简历',
      type: Sequelize.STRING(50),
      allowNull: true,
      defaultValue: ''
    },
    
    // 其他信息
    error_message: {
      comment: '错误信息',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    notes: {
      comment: '备注',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // 时间戳
    create_time: {
      comment: '创建时间',
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    },
    update_time: {
      comment: '更新时间',
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    },
    is_delete: {
      comment: '是否删除',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    }
  }, {
    timestamps: false,
    indexes: [
      {
        unique: false,
        fields: ['user_id']
      },
      {
        unique: false,
        fields: ['message_type']
      },
      {
        unique: false,
        fields: ['status']
      },
      {
        unique: false,
        fields: ['create_time']
      },
      {
        unique: false,
        fields: ['is_delete']
      }
    ]
  });

  return ai_messages;
};

