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

/**
 * 待安装任务模型
 * 用于管理保险设备安装任务相关的数据
 */
const InstallationTask = sequelize.define('InstallationTask', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '主键ID'
  },
  applicationNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    field: 'application_number',
    comment: '申请单号'
  },
  policyNumber: {
    type: DataTypes.STRING(50),
    allowNull: false,
    field: 'policy_number',
    comment: '保单编号'
  },
  productName: {
    type: DataTypes.STRING(100),
    allowNull: false,
    field: 'product_name',
    comment: '产品名称'
  },
  customerName: {
    type: DataTypes.STRING(50),
    allowNull: false,
    field: 'customer_name',
    comment: '客户姓名'
  },
  idType: {
    type: DataTypes.ENUM('身份证', '护照', '军官证', '士兵证', '港澳台居民居住证', '其他'),
    allowNull: false,
    defaultValue: '身份证',
    field: 'id_type',
    comment: '证件类型'
  },
  idNumber: {
    type: DataTypes.STRING(30),
    allowNull: false,
    field: 'id_number',
    comment: '证件号码'
  },
  livestockSupplyType: {
    type: DataTypes.STRING(100),
    allowNull: true,
    field: 'livestock_supply_type',
    comment: '养殖生资种类'
  },
  pendingDevices: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'pending_devices',
    comment: '待安装设备（JSON格式存储）'
  },
  installationStatus: {
    type: DataTypes.ENUM('待安装', '安装中', '已安装', '安装失败', '已取消'),
    allowNull: false,
    defaultValue: '待安装',
    field: 'installation_status',
    comment: '安装状态'
  },
  taskGeneratedTime: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'task_generated_time',
    comment: '生成安装任务时间'
  },
  installationCompletedTime: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'installation_completed_time',
    comment: '安装完成生效时间'
  },
  priority: {
    type: DataTypes.ENUM('低', '中', '高', '紧急'),
    allowNull: false,
    defaultValue: '中',
    comment: '任务优先级'
  },
  assignedTo: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'assigned_technician',
    comment: '分配给（用户ID）'
  },
  createdBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'created_by',
    comment: '创建人ID'
  },
  updatedBy: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'updated_by',
    comment: '更新人ID'
  }
}, {
  tableName: 'installation_tasks',
  timestamps: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

module.exports = InstallationTask;