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

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"plumbing",


"electrical",


"hvac",


"appliance",


"furniture",


"internet_tv",


"other"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_progress",


"closed"

        ],

      },

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

  maintenance_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.maintenance_tickets.belongsTo(db.hotels, {
      as: 'hotel',
      foreignKey: {
        name: 'hotelId',
      },
      constraints: false,
    });

    db.maintenance_tickets.belongsTo(db.rooms, {
      as: 'room',
      foreignKey: {
        name: 'roomId',
      },
      constraints: false,
    });

    db.maintenance_tickets.belongsTo(db.staff_profiles, {
      as: 'assigned_staff_profile',
      foreignKey: {
        name: 'assigned_staff_profileId',
      },
      constraints: false,
    });

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



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


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

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



  return maintenance_tickets;
};


