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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"damage",


"missing_quantity",


"wrong_item",


"quality_issue",


"documentation_issue",


"other"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_review",


"resolved",


"cancelled"

        ],

      },

reported_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

resolution_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  discrepancy_cases.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity




































//end loop



    db.discrepancy_cases.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.discrepancy_cases.belongsTo(db.purchase_orders, {
      as: 'purchase_order',
      foreignKey: {
        name: 'purchase_orderId',
      },
      constraints: false,
    });

    db.discrepancy_cases.belongsTo(db.goods_receipt_notes, {
      as: 'goods_receipt_note',
      foreignKey: {
        name: 'goods_receipt_noteId',
      },
      constraints: false,
    });

    db.discrepancy_cases.belongsTo(db.users, {
      as: 'reported_by',
      foreignKey: {
        name: 'reported_byId',
      },
      constraints: false,
    });

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



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


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

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



  return discrepancy_cases;
};


