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

work_order_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"released",


"in_progress",


"on_hold",


"completed",


"closed",


"cancelled"

        ],

      },

planned_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

completed_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

scrap_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

scheduled_start: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end: {
        type: DataTypes.DATE,
      
      

      },

released_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

priority: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  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


















    db.work_orders.hasMany(db.machine_events, {
      as: 'machine_events_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });




    db.work_orders.hasMany(db.work_order_operations, {
      as: 'work_order_operations_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });





    db.work_orders.hasMany(db.inventory_movements, {
      as: 'inventory_movements_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });






    db.work_orders.hasMany(db.inspections, {
      as: 'inspections_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });



    db.work_orders.hasMany(db.nonconformances, {
      as: 'nonconformances_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });






//end loop



    db.work_orders.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

    db.work_orders.belongsTo(db.boms, {
      as: 'bom',
      foreignKey: {
        name: 'bomId',
      },
      constraints: false,
    });

    db.work_orders.belongsTo(db.routings, {
      as: 'routing',
      foreignKey: {
        name: 'routingId',
      },
      constraints: false,
    });

    db.work_orders.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });




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

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



  return work_orders;
};


