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,
      
      

      },

scheduled_start: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end: {
        type: DataTypes.DATE,
      
      

      },

actual_start: {
        type: DataTypes.DATE,
      
      

      },

actual_end: {
        type: DataTypes.DATE,
      
      

      },

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







    db.work_orders.hasMany(db.machine_downtime_events, {
      as: 'machine_downtime_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.material_issues, {
      as: 'material_issues_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.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.routes, {
      as: 'route',
      foreignKey: {
        name: 'routeId',
      },
      constraints: false,
    });

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

    db.work_orders.belongsTo(db.uoms, {
      as: 'uom',
      foreignKey: {
        name: 'uomId',
      },
      constraints: false,
    });

    db.work_orders.belongsTo(db.production_lines, {
      as: 'production_line',
      foreignKey: {
        name: 'production_lineId',
      },
      constraints: false,
    });

    db.work_orders.belongsTo(db.machines, {
      as: 'primary_machine',
      foreignKey: {
        name: 'primary_machineId',
      },
      constraints: false,
    });

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



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


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

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



  return work_orders;
};


