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

name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

default_language: {
        type: DataTypes.ENUM,
      
      

        values: [

"en",


"vi"

        ],

      },

default_target_headcount: {
        type: DataTypes.INTEGER,
      
      

      },

payment_reminder_days: {
        type: DataTypes.INTEGER,
      
      

      },

inactive_nudge_last_games: {
        type: DataTypes.INTEGER,
      
      

      },

messenger_group_thread: {
        type: DataTypes.TEXT,
      
      

      },

messenger_access_token: {
        type: DataTypes.TEXT,
      
      

      },

stripe_account_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  clubs.associate = (db) => {


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







    db.clubs.hasMany(db.club_memberships, {
      as: 'club_memberships_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });


    db.clubs.hasMany(db.players, {
      as: 'players_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });


    db.clubs.hasMany(db.seed_votes, {
      as: 'seed_votes_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });


    db.clubs.hasMany(db.venues, {
      as: 'venues_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });


    db.clubs.hasMany(db.leagues, {
      as: 'leagues_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });




    db.clubs.hasMany(db.games, {
      as: 'games_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });












    db.clubs.hasMany(db.notifications, {
      as: 'notifications_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });


    db.clubs.hasMany(db.audit_events, {
      as: 'audit_events_club',
      foreignKey: {
          name: 'clubId',
      },
      constraints: false,
    });



//end loop



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



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


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

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



  return clubs;
};


