const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const LoanProduct = sequelize.define('LoanProduct', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '主键ID'
  },
  productName: {
    type: DataTypes.STRING(200),
    allowNull: false,
    field: 'product_name',
    comment: '贷款产品名称'
  },
  loanAmount: {
    type: DataTypes.STRING(100),
    allowNull: false,
    field: 'loan_amount',
    comment: '贷款额度'
  },
  loanTerm: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'loan_term',
    comment: '贷款周期（月）'
  },
  interestRate: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    field: 'interest_rate',
    comment: '贷款利率（%）'
  },
  serviceArea: {
    type: DataTypes.STRING(200),
    allowNull: false,
    field: 'service_area',
    comment: '服务区域'
  },
  servicePhone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    field: 'service_phone',
    comment: '服务电话'
  },
  totalCustomers: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'total_customers',
    comment: '服务客户总数量'
  },
  supervisionCustomers: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'supervision_customers',
    comment: '监管中客户数量'
  },
  completedCustomers: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'completed_customers',
    comment: '已结项客户数量'
  },
  onSaleStatus: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: true,
    field: 'on_sale_status',
    comment: '在售状态'
  },
  productDescription: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'product_description',
    comment: '产品描述'
  },
  applicationRequirements: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'application_requirements',
    comment: '申请条件'
  },
  requiredDocuments: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'required_documents',
    comment: '所需材料'
  },
  approvalProcess: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'approval_process',
    comment: '审批流程'
  },
  riskLevel: {
    type: DataTypes.ENUM('LOW', 'MEDIUM', 'HIGH'),
    allowNull: false,
    defaultValue: 'MEDIUM',
    field: 'risk_level',
    comment: '风险等级'
  },
  minLoanAmount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: true,
    field: 'min_loan_amount',
    comment: '最小贷款金额'
  },
  maxLoanAmount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: true,
    field: 'max_loan_amount',
    comment: '最大贷款金额'
  },
  createdBy: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'created_by',
    comment: '创建人ID'
  },
  updatedBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'updated_by',
    comment: '更新人ID'
  }
}, {
  tableName: 'loan_products',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  comment: '贷款商品表'
});

module.exports = LoanProduct;