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 outlets = sequelize.define(
    'outlets',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

gps_lat: {
        type: DataTypes.DECIMAL,
      
      

      },

gps_lng: {
        type: DataTypes.DECIMAL,
      
      

      },

gps_radius_m: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  outlets.associate = (db) => {


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







    db.outlets.hasMany(db.job_positions, {
      as: 'job_positions_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.employees, {
      as: 'employees_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });



    db.outlets.hasMany(db.attendance_logs, {
      as: 'attendance_logs_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.attendance_requests, {
      as: 'attendance_requests_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.overtime_logs, {
      as: 'overtime_logs_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.disciplinary_actions, {
      as: 'disciplinary_actions_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.kpi_periods, {
      as: 'kpi_periods_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.kpi_scores, {
      as: 'kpi_scores_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.outlet_revenues, {
      as: 'outlet_revenues_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.service_charge_pools, {
      as: 'service_charge_pools_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.loan_accounts, {
      as: 'loan_accounts_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.payroll_periods, {
      as: 'payroll_periods_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });


    db.outlets.hasMany(db.payroll_items, {
      as: 'payroll_items_outlet',
      foreignKey: {
          name: 'outletId',
      },
      constraints: false,
    });







//end loop



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




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

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



  return outlets;
};


