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,
      
      

      },

branch_code: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address_line: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

region: {
        type: DataTypes.TEXT,
      
      

      },

is_active: {
        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.user_roles, {
      as: 'user_roles_branch',
      foreignKey: {
          name: 'branchId',
      },
      constraints: false,
    });


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




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



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



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


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



    db.branches.hasMany(db.interbranch_transfers, {
      as: 'interbranch_transfers_from_branch',
      foreignKey: {
          name: 'from_branchId',
      },
      constraints: false,
    });

    db.branches.hasMany(db.interbranch_transfers, {
      as: 'interbranch_transfers_to_branch',
      foreignKey: {
          name: 'to_branchId',
      },
      constraints: false,
    });





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



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






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


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


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





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



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



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



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


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



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


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





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



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


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


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



    db.branches.hasMany(db.ai_reorder_suggestions, {
      as: 'ai_reorder_suggestions_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;
};


