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


"qc_hold",


"completed",


"cancelled"

        ],

      },

scheduled_start_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end_at: {
        type: DataTypes.DATE,
      
      

      },

actual_start_at: {
        type: DataTypes.DATE,
      
      

      },

actual_end_at: {
        type: DataTypes.DATE,
      
      

      },

planned_qty: {
        type: DataTypes.DECIMAL,
      
      

      },

completed_qty: {
        type: DataTypes.DECIMAL,
      
      

      },

instructions: {
        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.work_order_materials, {
      as: 'work_order_materials_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });









//end loop



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

    db.work_orders.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

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

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




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

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



  return work_orders;
};


