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

ticket_number: {
        type: DataTypes.TEXT,
      
      

      },

subject: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"email",


"whatsapp",


"phone",


"portal",


"internal"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"p1",


"p2",


"p3",


"p4"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_progress",


"waiting",


"resolved",


"closed"

        ],

      },

first_response_due_at: {
        type: DataTypes.DATE,
      
      

      },

resolution_due_at: {
        type: DataTypes.DATE,
      
      

      },

first_response_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

      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







































    db.support_tickets.hasMany(db.ticket_comments, {
      as: 'ticket_comments_ticket',
      foreignKey: {
          name: 'ticketId',
      },
      constraints: false,
    });


    db.support_tickets.hasMany(db.csat_surveys, {
      as: 'csat_surveys_ticket',
      foreignKey: {
          name: 'ticketId',
      },
      constraints: false,
    });







//end loop



    db.support_tickets.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.support_tickets.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.support_tickets.belongsTo(db.contacts, {
      as: 'contact',
      foreignKey: {
        name: 'contactId',
      },
      constraints: false,
    });

    db.support_tickets.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.support_tickets.belongsTo(db.contracts, {
      as: 'contract',
      foreignKey: {
        name: 'contractId',
      },
      constraints: false,
    });

    db.support_tickets.belongsTo(db.service_lines, {
      as: 'service_line',
      foreignKey: {
        name: 'service_lineId',
      },
      constraints: false,
    });

    db.support_tickets.belongsTo(db.support_slas, {
      as: 'sla',
      foreignKey: {
        name: 'slaId',
      },
      constraints: false,
    });

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

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



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


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

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



  return support_tickets;
};


