const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const branches = sequelize.define(
    'branches',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

branch_name: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.ENUM,
      
      

        values: [

"andaman_and_nicobar_islands",


"andhra_pradesh",


"arunachal_pradesh",


"assam",


"bihar",


"chandigarh",


"chhattisgarh",


"dadra_and_nagar_haveli_and_daman_and_diu",


"delhi",


"goa",


"gujarat",


"haryana",


"himachal_pradesh",


"jammu_and_kashmir",


"jharkhand",


"karnataka",


"kerala",


"ladakh",


"lakshadweep",


"madhya_pradesh",


"maharashtra",


"manipur",


"meghalaya",


"mizoram",


"nagaland",


"odisha",


"puducherry",


"punjab",


"rajasthan",


"sikkim",


"tamil_nadu",


"telangana",


"tripura",


"uttar_pradesh",


"uttarakhand",


"west_bengal"

        ],

      },

pincode: {
        type: DataTypes.TEXT,
      
      

      },

gst_registration_number: {
        type: DataTypes.TEXT,
      
      

      },

is_head_office: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  branches.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity












    db.branches.hasMany(db.company_compliances, {
      as: 'company_compliances_branch',
      foreignKey: {
          name: 'branchId',
      },
      constraints: false,
    });


    db.branches.hasMany(db.tasks, {
      as: 'tasks_branch',
      foreignKey: {
          name: 'branchId',
      },
      constraints: false,
    });


    db.branches.hasMany(db.documents, {
      as: 'documents_branch',
      foreignKey: {
          name: 'branchId',
      },
      constraints: false,
    });













    db.branches.hasMany(db.compliance_rule_overrides, {
      as: 'compliance_rule_overrides_branch',
      foreignKey: {
          name: 'branchId',
      },
      constraints: false,
    });





//end loop



    db.branches.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.branches.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




    db.branches.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.branches.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return branches;
};


