module.exports = (sequelize, DataTypes) => {
  const InProcessCheckList = sequelize.define('InProcessCheckList', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    ipc_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      comment: 'In-Process CheckList Number'
    },
    ipc_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      comment: 'CheckList Date'
    },
    doc_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      defaultValue: 'RI/QA/F/09',
      comment: 'Document Number'
    },
    rev_no: {
      type: DataTypes.STRING(20),
      allowNull: true,
      defaultValue: '0 / 05.10.2018',
      comment: 'Revision Number'
    },
    work_order_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Work Order Number (WON)'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    customer_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Customer name (cached)'
    },
    model: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Model/Product'
    },
    total_qty: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Total Quantity'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT',
      comment: 'Document status'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'in_process_checklist',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['plant_id'] },
      { unique: true, fields: ['ipc_no'] },
      { fields: ['ipc_date'] },
      { fields: ['customer_id'] },
      { fields: ['status'] }
    ]
  });

  InProcessCheckList.associate = function(models) {
    InProcessCheckList.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    InProcessCheckList.belongsTo(models.Plant, {
      foreignKey: 'plant_id',
      as: 'plant'
    });

    InProcessCheckList.belongsTo(models.FiscalYear, {
      foreignKey: 'fiscal_year_id',
      as: 'fiscalYear'
    });

    InProcessCheckList.belongsTo(models.BusinessPartner, {
      foreignKey: 'customer_id',
      as: 'customer'
    });

    InProcessCheckList.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    InProcessCheckList.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });

    InProcessCheckList.hasMany(models.InProcessCheckListDetail, {
      foreignKey: 'ipc_id',
      as: 'details'
    });
  };

  return InProcessCheckList;
};
