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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

  organizations.associate = (db) => {


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


    db.organizations.hasMany(db.users, {
      as: 'users_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });





    db.organizations.hasMany(db.workspaces, {
      as: 'workspaces_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.workspace_memberships, {
      as: 'workspace_memberships_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.ai_models, {
      as: 'ai_models_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.ai_agents, {
      as: 'ai_agents_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.agent_skills, {
      as: 'agent_skills_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.agent_skill_assignments, {
      as: 'agent_skill_assignments_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.conversations, {
      as: 'conversations_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.messages, {
      as: 'messages_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.message_attachments, {
      as: 'message_attachments_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.code_snippets, {
      as: 'code_snippets_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.executions, {
      as: 'executions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.debug_sessions, {
      as: 'debug_sessions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.debug_fixes, {
      as: 'debug_fixes_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.projects, {
      as: 'projects_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.project_files, {
      as: 'project_files_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.previews, {
      as: 'previews_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.tool_integrations, {
      as: 'tool_integrations_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.api_keys, {
      as: 'api_keys_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.usage_events, {
      as: 'usage_events_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.audit_logs, {
      as: 'audit_logs_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });



//end loop






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

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



  return organizations;
};


