/**
 * 监管任务模型
 * @file SupervisionTask.js
 * @description 监管任务数据模型定义
 */
const { DataTypes } = require('sequelize');
const BaseModel = require('./BaseModel');
const { sequelize } = require('../config/database');

class SupervisionTask extends BaseModel {}

SupervisionTask.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  applicationNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '申请单号',
    field: 'applicationNumber'
  },
  contractNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '放款合同编号'
  },
  productName: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '产品名称'
  },
  customerName: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '客户姓名'
  },
  idType: {
    type: DataTypes.ENUM('id_card', 'passport', 'other'),
    allowNull: false,
    defaultValue: 'id_card',
    comment: '证件类型：id_card-身份证，passport-护照，other-其他'
  },
  idNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '证件号码'
  },
  assetType: {
    type: DataTypes.ENUM('cattle', 'sheep', 'pig', 'poultry', 'other'),
    allowNull: false,
    defaultValue: 'cattle',
    comment: '养殖生资种类：cattle-牛，sheep-羊，pig-猪，poultry-家禽，other-其他'
  },
  assetQuantity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '监管生资数量'
  },
  supervisionStatus: {
    type: DataTypes.ENUM('pending', 'supervising', 'completed', 'suspended'),
    allowNull: false,
    defaultValue: 'pending',
    comment: '监管状态：pending-待监管，supervising-监管中，completed-已完成，suspended-已暂停'
  },
  importTime: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '任务导入时间'
  },
  startTime: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    comment: '监管起始时间'
  },
  endTime: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    comment: '监管结束时间'
  },
  loanAmount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: true,
    defaultValue: 0.00,
    comment: '贷款金额'
  },
  interestRate: {
    type: DataTypes.DECIMAL(5, 4),
    allowNull: true,
    defaultValue: 0.0000,
    comment: '利率'
  },
  loanTerm: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 12,
    comment: '贷款期限（月）'
  },
  supervisorName: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '监管员姓名'
  },
  supervisorPhone: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: '监管员电话'
  },
  farmAddress: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: '养殖场地址'
  },
  remarks: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注'
  },
  createdBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID'
  },
  updatedBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID'
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  }
}, {
  sequelize,
  modelName: 'SupervisionTask',
  tableName: 'supervision_tasks',
  timestamps: true,
  underscored: false,
  createdAt: 'createdAt',
  updatedAt: 'updatedAt',
  comment: '监管任务表'
});

// 定义关联关系
SupervisionTask.associate = (models) => {
  SupervisionTask.belongsTo(models.User, {
    foreignKey: 'createdBy',
    as: 'creator',
    targetKey: 'id'
  });
  SupervisionTask.belongsTo(models.User, {
    foreignKey: 'updatedBy',
    as: 'updater',
    targetKey: 'id'
  });
};

module.exports = SupervisionTask;
