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

name: {
        type: DataTypes.TEXT,
      
      

      },

store_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"closed",


"open",


"renovating"

        ],

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

cash_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

pix_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

card_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

store_note: {
        type: DataTypes.TEXT,
      
      

      },

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

  market_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.market_stores.hasMany(db.employees, {
      as: 'employees_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.market_stores.hasMany(db.store_products, {
      as: 'store_products_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



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



    db.market_stores.hasMany(db.deliveries, {
      as: 'deliveries_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.market_stores.hasMany(db.shelves, {
      as: 'shelves_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.market_stores.hasMany(db.cash_registers, {
      as: 'cash_registers_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.market_stores.hasMany(db.sales_transactions, {
      as: 'sales_transactions_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.market_stores.hasMany(db.daily_reports, {
      as: 'daily_reports_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



















//end loop



    db.market_stores.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });



    db.market_stores.hasMany(db.file, {
      as: 'store_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.market_stores.getTableName(),
        belongsToColumn: 'store_images',
      },
    });


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

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



  return market_stores;
};


