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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

emirate: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

vat_trn: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

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

  stores.associate = (db) => {


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











    db.stores.hasMany(db.price_lists, {
      as: 'price_lists_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.stores.hasMany(db.discounts, {
      as: 'discounts_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.stores.hasMany(db.registers, {
      as: 'registers_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.stores.hasMany(db.sales, {
      as: 'sales_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });




    db.stores.hasMany(db.returns, {
      as: 'returns_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });




    db.stores.hasMany(db.purchase_orders, {
      as: 'purchase_orders_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.stores.hasMany(db.inventory_items, {
      as: 'inventory_items_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.stores.hasMany(db.stock_movements, {
      as: 'stock_movements_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.stores.hasMany(db.stock_transfers, {
      as: 'stock_transfers_from_store',
      foreignKey: {
          name: 'from_storeId',
      },
      constraints: false,
    });

    db.stores.hasMany(db.stock_transfers, {
      as: 'stock_transfers_to_store',
      foreignKey: {
          name: 'to_storeId',
      },
      constraints: false,
    });




//end loop



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




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

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



  return stores;
};


