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

company_name: {
        type: DataTypes.TEXT,
      
      

      },

business_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"proprietorship",


"partnership",


"llp",


"private_limited",


"public_limited",


"opc",


"huf",


"trust",


"society",


"ngo",


"other"

        ],

      },

industry: {
        type: DataTypes.TEXT,
      
      

      },

gst_number: {
        type: DataTypes.TEXT,
      
      

      },

pan_number: {
        type: DataTypes.TEXT,
      
      

      },

cin_number: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.ENUM,
      
      

        values: [

"andaman_and_nicobar_islands",


"andhra_pradesh",


"arunachal_pradesh",


"assam",


"bihar",


"chandigarh",


"chhattisgarh",


"dadra_and_nagar_haveli_and_daman_and_diu",


"delhi",


"goa",


"gujarat",


"haryana",


"himachal_pradesh",


"jammu_and_kashmir",


"jharkhand",


"karnataka",


"kerala",


"ladakh",


"lakshadweep",


"madhya_pradesh",


"maharashtra",


"manipur",


"meghalaya",


"mizoram",


"nagaland",


"odisha",


"puducherry",


"punjab",


"rajasthan",


"sikkim",


"tamil_nadu",


"telangana",


"tripura",


"uttar_pradesh",


"uttarakhand",


"west_bengal"

        ],

      },

employee_count: {
        type: DataTypes.INTEGER,
      
      

      },

compliance_score: {
        type: DataTypes.DECIMAL,
      
      

      },

risk_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  companies.associate = (db) => {


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







    db.companies.hasMany(db.company_memberships, {
      as: 'company_memberships_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.branches, {
      as: 'branches_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.departments, {
      as: 'departments_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });




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


    db.companies.hasMany(db.tasks, {
      as: 'tasks_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.documents, {
      as: 'documents_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });



    db.companies.hasMany(db.alerts, {
      as: 'alerts_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.calendar_events, {
      as: 'calendar_events_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });



    db.companies.hasMany(db.company_subscriptions, {
      as: 'company_subscriptions_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.payments, {
      as: 'payments_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.ai_chat_sessions, {
      as: 'ai_chat_sessions_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });



    db.companies.hasMany(db.audit_logs, {
      as: 'audit_logs_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.activity_events, {
      as: 'activity_events_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.report_exports, {
      as: 'report_exports_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.compliance_rule_overrides, {
      as: 'compliance_rule_overrides_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.integrations, {
      as: 'integrations_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.notifications_preferences, {
      as: 'notifications_preferences_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });



//end loop



    db.companies.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });

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




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

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



  return companies;
};


