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

title: {
        type: DataTypes.TEXT,
      
      

      },

language: {
        type: DataTypes.ENUM,
      
      

        values: [

"javascript",


"typescript",


"python",


"java",


"csharp",


"cpp",


"go",


"rust",


"php",


"ruby",


"swift",


"kotlin",


"html",


"css",


"sql",


"bash",


"yaml",


"json",


"markdown",


"other"

        ],

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

entry_point: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  code_snippets.associate = (db) => {


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
















    db.code_snippets.hasMany(db.executions, {
      as: 'executions_snippet',
      foreignKey: {
          name: 'snippetId',
      },
      constraints: false,
    });


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











//end loop



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

    db.code_snippets.belongsTo(db.users, {
      as: 'author',
      foreignKey: {
        name: 'authorId',
      },
      constraints: false,
    });

    db.code_snippets.belongsTo(db.messages, {
      as: 'message',
      foreignKey: {
        name: 'messageId',
      },
      constraints: false,
    });

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




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

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



  return code_snippets;
};


