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

compliance_title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"not_started",


"in_progress",


"submitted",


"completed",


"overdue",


"waived"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

risk_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

period_start_at: {
        type: DataTypes.DATE,
      
      

      },

period_end_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

penalty_estimate: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  company_compliances.associate = (db) => {


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













    db.company_compliances.hasMany(db.tasks, {
      as: 'tasks_related_compliance',
      foreignKey: {
          name: 'related_complianceId',
      },
      constraints: false,
    });


    db.company_compliances.hasMany(db.documents, {
      as: 'documents_related_compliance',
      foreignKey: {
          name: 'related_complianceId',
      },
      constraints: false,
    });



    db.company_compliances.hasMany(db.alerts, {
      as: 'alerts_related_compliance',
      foreignKey: {
          name: 'related_complianceId',
      },
      constraints: false,
    });


    db.company_compliances.hasMany(db.calendar_events, {
      as: 'calendar_events_related_compliance',
      foreignKey: {
          name: 'related_complianceId',
      },
      constraints: false,
    });














//end loop



    db.company_compliances.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.company_compliances.belongsTo(db.compliance_templates, {
      as: 'template',
      foreignKey: {
        name: 'templateId',
      },
      constraints: false,
    });

    db.company_compliances.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.company_compliances.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      constraints: false,
    });

    db.company_compliances.belongsTo(db.users, {
      as: 'assigned_to',
      foreignKey: {
        name: 'assigned_toId',
      },
      constraints: false,
    });

    db.company_compliances.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });

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




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

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



  return company_compliances;
};


