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

subject: {
        type: DataTypes.TEXT,
      
      

      },

message: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"pending",


"resolved",


"closed"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  support_tickets.associate = (db) => {


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




























//end loop



    db.support_tickets.belongsTo(db.users, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });



    db.support_tickets.hasMany(db.file, {
      as: 'attachment_files',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.support_tickets.getTableName(),
        belongsToColumn: 'attachment_files',
      },
    });


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

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



  return support_tickets;
};


