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,
      
      

      },

quantity_planned: {
        type: DataTypes.DECIMAL,
      
      

      },

quantity_completed: {
        type: DataTypes.DECIMAL,
      
      

      },

uom: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"released",


"in_progress",


"qa_hold",


"completed",


"closed",


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

      },

customer_po: {
        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.production_operations, {
      as: 'production_operations_work_order',
      foreignKey: {
          name: 'work_orderId',
      },
      constraints: false,
    });


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


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




    db.work_orders.hasMany(db.qa_inspections, {
      as: 'qa_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.plants, {
      as: 'plant',
      foreignKey: {
        name: 'plantId',
      },
      constraints: false,
    });

    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.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

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




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

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



  return work_orders;
};


