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

provider_account_email: {
        type: DataTypes.TEXT,
      
      

      },

access_token_encrypted: {
        type: DataTypes.TEXT,
      
      

      },

refresh_token_encrypted: {
        type: DataTypes.TEXT,
      
      

      },

token_expires_at: {
        type: DataTypes.DATE,
      
      

      },

sync_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"enabled",


"paused",


"error"

        ],

      },

last_synced_at: {
        type: DataTypes.DATE,
      
      

      },

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

  calendar_accounts.associate = (db) => {


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



























    db.calendar_accounts.hasMany(db.calendar_events, {
      as: 'calendar_events_calendar_account',
      foreignKey: {
          name: 'calendar_accountId',
      },
      constraints: false,
    });




//end loop



    db.calendar_accounts.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.calendar_accounts.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.calendar_accounts.belongsTo(db.integrations, {
      as: 'integration',
      foreignKey: {
        name: 'integrationId',
      },
      constraints: false,
    });

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




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

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



  return calendar_accounts;
};


