/**
 * 贷款解押模型
 * @file LoanRelease.js
 * @description 银行系统贷款解押数据模型
 */
const { DataTypes } = require('sequelize');
const BaseModel = require('./BaseModel');

class LoanRelease extends BaseModel {
  /**
   * 获取解押状态文本
   * @returns {String} 状态文本
   */
  getStatusText() {
    const statusMap = {
      pending: '待处理',
      processing: '处理中',
      approved: '已通过',
      rejected: '已拒绝',
      completed: '已完成',
      released: '已解押'
    };
    return statusMap[this.status] || this.status;
  }

  /**
   * 获取解押状态颜色
   * @returns {String} 颜色
   */
  getStatusColor() {
    const colorMap = {
      pending: 'orange',
      processing: 'blue',
      approved: 'green',
      rejected: 'red',
      completed: 'green',
      released: 'default'
    };
    return colorMap[this.status] || 'default';
  }

  /**
   * 格式化解押金额
   * @returns {String} 格式化后的金额
   */
  getFormattedAmount() {
    return `${this.release_amount.toFixed(2)}元`;
  }

  /**
   * 获取抵押物类型文本
   * @returns {String} 类型文本
   */
  getCollateralTypeText() {
    const typeMap = {
      house: '房产',
      car: '车辆',
      land: '土地',
      equipment: '设备',
      livestock: '牲畜'
    };
    return typeMap[this.collateral_type] || this.collateral_type;
  }
}

// 初始化LoanRelease模型
LoanRelease.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  application_number: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '解押申请单号'
  },
  contract_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '关联合同ID'
  },
  customer_name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '客户姓名'
  },
  customer_phone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '客户电话'
  },
  customer_id_card: {
    type: DataTypes.STRING(18),
    allowNull: false,
    comment: '客户身份证号'
  },
  farmer_name: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '养殖户姓名'
  },
  product_name: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: '贷款产品名称'
  },
  collateral_type: {
    type: DataTypes.ENUM('house', 'car', 'land', 'equipment', 'livestock'),
    allowNull: false,
    comment: '抵押物类型'
  },
  collateral_description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '抵押物描述'
  },
  collateral_value: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: true,
    comment: '抵押物价值'
  },
  loan_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: true,
    comment: '贷款金额'
  },
  release_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    comment: '解押金额'
  },
  release_quantity: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '解押数量'
  },
  application_reason: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '申请原因'
  },
  status: {
    type: DataTypes.ENUM('pending', 'processing', 'approved', 'rejected', 'completed', 'released'),
    allowNull: false,
    defaultValue: 'pending',
    comment: '解押状态'
  },
  application_date: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '申请日期'
  },
  process_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '处理日期'
  },
  complete_date: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '完成日期'
  },
  processor_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '处理人ID'
  },
  processor_name: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '处理人姓名'
  },
  process_comment: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '处理意见'
  },
  rejection_reason: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '拒绝原因'
  },
  remark: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注'
  }
}, {
  sequelize: require('../config/database').sequelize,
  modelName: 'LoanRelease',
  tableName: 'bank_loan_releases',
  timestamps: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

module.exports = LoanRelease;
