const { DataTypes } = require('sequelize')
const BaseModel = require('./BaseModel')

class FormLog extends BaseModel {
  static init(sequelize) {
    return super.init({
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '日志ID'
      },
      module: {
        type: DataTypes.STRING(100),
        allowNull: false,
        comment: '模块名称'
      },
      action: {
        type: DataTypes.STRING(100),
        allowNull: false,
        comment: '操作类型'
      },
      userId: {
        type: DataTypes.INTEGER,
        allowNull: true,
        field: 'userId',
        comment: '用户ID'
      },
      username: {
        type: DataTypes.STRING(100),
        allowNull: true,
        field: 'username',
        comment: '用户名'
      },
      sessionId: {
        type: DataTypes.STRING(255),
        allowNull: true,
        field: 'sessionId',
        comment: '会话ID'
      },
      userAgent: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'userAgent',
        comment: '用户代理'
      },
      screenResolution: {
        type: DataTypes.STRING(50),
        allowNull: true,
        field: 'screenResolution',
        comment: '屏幕分辨率'
      },
      currentUrl: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'currentUrl',
        comment: '当前URL'
      },
      formData: {
        type: DataTypes.JSON,
        allowNull: true,
        field: 'formData',
        comment: '表单数据'
      },
      additionalData: {
        type: DataTypes.JSON,
        allowNull: true,
        field: 'additionalData',
        comment: '附加数据'
      },
      ipAddress: {
        type: DataTypes.STRING(45),
        allowNull: true,
        field: 'ipAddress',
        comment: 'IP地址'
      },
      timestamp: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        field: 'timestamp',
        comment: '时间戳'
      },
      status: {
        type: DataTypes.ENUM('success', 'error', 'warning'),
        allowNull: false,
        defaultValue: 'success',
        field: 'status',
        comment: '状态'
      },
      errorMessage: {
        type: DataTypes.TEXT,
        allowNull: true,
        field: 'errorMessage',
        comment: '错误信息'
      }
    }, {
      sequelize,
      modelName: 'FormLog',
      tableName: 'form_logs',
      comment: '表单操作日志表',
      timestamps: false, // 禁用自动时间戳
      indexes: [
        {
          fields: ['module', 'action']
        },
        {
          fields: ['userId']
        },
        {
          fields: ['timestamp']
        },
        {
          fields: ['status']
        }
      ]
    })
  }

  static associate(models) {
    // 可以添加与其他模型的关联
  }
}

module.exports = FormLog
