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,
      },

      category: {
        type: DataTypes.ENUM,

        values: ['billing', 'technical', 'general'],
      },

      priority: {
        type: DataTypes.ENUM,

        values: ['low', 'medium', 'high', 'urgent'],
      },

      status: {
        type: DataTypes.ENUM,

        values: ['open', 'in_progress', 'resolved', 'escalated'],
      },

      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.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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

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

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

  return support_tickets;
};
