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

name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"planning",


"confirmed",


"in_progress",


"completed",


"canceled"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

budget_total: {
        type: DataTypes.DECIMAL,
      
      

      },

budget_spent: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  events.associate = (db) => {


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
























    db.events.hasMany(db.event_tasks, {
      as: 'event_tasks_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });


    db.events.hasMany(db.event_guests, {
      as: 'event_guests_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });


    db.events.hasMany(db.event_schedule_items, {
      as: 'event_schedule_items_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });


    db.events.hasMany(db.budget_items, {
      as: 'budget_items_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });



//end loop



    db.events.belongsTo(db.venues, {
      as: 'venue',
      foreignKey: {
        name: 'venueId',
      },
      constraints: false,
    });

    db.events.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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



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


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

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



  return events;
};


