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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

order_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"mto",


"mts",


"rework",


"trial"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"released",


"in_progress",


"on_hold",


"completed",


"closed",


"cancelled"

        ],

      },

planned_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

completed_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

planned_start_at: {
        type: DataTypes.DATE,
      
      

      },

planned_end_at: {
        type: DataTypes.DATE,
      
      

      },

released_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  production_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.production_orders.hasMany(db.work_order_operations, {
      as: 'work_order_operations_production_order',
      foreignKey: {
          name: 'production_orderId',
      },
      constraints: false,
    });



    db.production_orders.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_production_order',
      foreignKey: {
          name: 'production_orderId',
      },
      constraints: false,
    });






    db.production_orders.hasMany(db.inspections, {
      as: 'inspections_production_order',
      foreignKey: {
          name: 'production_orderId',
      },
      constraints: false,
    });


    db.production_orders.hasMany(db.nonconformances, {
      as: 'nonconformances_production_order',
      foreignKey: {
          name: 'production_orderId',
      },
      constraints: false,
    });



    db.production_orders.hasMany(db.alerts, {
      as: 'alerts_related_order',
      foreignKey: {
          name: 'related_orderId',
      },
      constraints: false,
    });




//end loop



    db.production_orders.belongsTo(db.plants, {
      as: 'plant',
      foreignKey: {
        name: 'plantId',
      },
      constraints: false,
    });

    db.production_orders.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      constraints: false,
    });

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

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

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

    db.production_orders.belongsTo(db.users, {
      as: 'planner_user',
      foreignKey: {
        name: 'planner_userId',
      },
      constraints: false,
    });



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


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

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



  return production_orders;
};


