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,
      
      

      },

planned_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

completed_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"released",


"in_progress",


"on_hold",


"completed",


"closed",


"cancelled"

        ],

      },

planned_start_at: {
        type: DataTypes.DATE,
      
      

      },

planned_end_at: {
        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,
    },
  );

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


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


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



    db.production_orders.hasMany(db.qa_inspections, {
      as: 'qa_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.production_costs, {
      as: 'production_costs_production_order',
      foreignKey: {
          name: 'production_orderId',
      },
      constraints: false,
    });






//end loop



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

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

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

    db.production_orders.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

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




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

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



  return production_orders;
};


