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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"running",


"resolved",


"closed"

        ],

      },

error_summary: {
        type: DataTypes.TEXT,
      
      

      },

error_details: {
        type: DataTypes.TEXT,
      
      

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  debug_sessions.associate = (db) => {


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


















    db.debug_sessions.hasMany(db.debug_fixes, {
      as: 'debug_fixes_debug_session',
      foreignKey: {
          name: 'debug_sessionId',
      },
      constraints: false,
    });










//end loop



    db.debug_sessions.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.debug_sessions.belongsTo(db.code_snippets, {
      as: 'snippet',
      foreignKey: {
        name: 'snippetId',
      },
      constraints: false,
    });

    db.debug_sessions.belongsTo(db.users, {
      as: 'started_by',
      foreignKey: {
        name: 'started_byId',
      },
      constraints: false,
    });

    db.debug_sessions.belongsTo(db.ai_agents, {
      as: 'assigned_agent',
      foreignKey: {
        name: 'assigned_agentId',
      },
      constraints: false,
    });

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




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

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



  return debug_sessions;
};


