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


"normal",


"high",


"urgent"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"requested",


"approved",


"scheduled",


"in_progress",


"waiting_parts",


"complete",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_start_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

problem_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.work_centers, {
      as: 'work_center',
      foreignKey: {
        name: 'work_centerId',
      },
      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.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;
};


