const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const ClientCollab = sequelize.define(
    "clientcollab",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },
      
      doc_no: {type: DataTypes.STRING, allowNull: true},
      start_date: {type: DataTypes.DATEONLY, allowNull: true},
      end_date: {type: DataTypes.DATEONLY, allowNull: true},
      territory_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: "territories", key: "id" },
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: "companies", key: "id" },
      },
    },
    {
      tableName: "clientcollabs",
    },
    {
      freezeTableName: true,
    }
  );

  ClientCollab.associate = function (models) {
    ClientCollab.belongsTo(models.Company,  { foreignKey: 'company_id', as: 'company' });
    ClientCollab.belongsTo(models.Territory,  { foreignKey: 'territory_id', as: 'territory' });
    ClientCollab.hasMany(models.ClientCollabDetail,  { foreignKey: 'clientcollab_id', as: 'clientcollabdetails' });
  }


  return ClientCollab;
};
