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

work_order_number: {
        type: DataTypes.TEXT,
      
      

      },

work_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"preventive",


"corrective",


"calibration",


"inspection"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"requested",


"approved",


"scheduled",


"in_progress",


"completed",


"closed",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_start: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

resolution_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  maintenance_work_orders.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_work_orders.belongsTo(db.machines, {
      as: 'machine',
      foreignKey: {
        name: 'machineId',
      },
      constraints: false,
    });

    db.maintenance_work_orders.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

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

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



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


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

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



  return maintenance_work_orders;
};


