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

receipt_kind: {
        type: DataTypes.ENUM,
      
      

        values: [

"sales",


"purchase",


"expense",


"other"

        ],

      },

source_channel: {
        type: DataTypes.TEXT,
      
      

      },

captured_at: {
        type: DataTypes.DATE,
      
      

      },

ocr_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"processing",


"completed",


"failed"

        ],

      },

ocr_confidence: {
        type: DataTypes.DECIMAL,
      
      

      },

needs_review: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  receipts.associate = (db) => {


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












    db.receipts.hasMany(db.ocr_extractions, {
      as: 'ocr_extractions_receipt',
      foreignKey: {
          name: 'receiptId',
      },
      constraints: false,
    });


    db.receipts.hasMany(db.sales_invoices, {
      as: 'sales_invoices_receipt',
      foreignKey: {
          name: 'receiptId',
      },
      constraints: false,
    });



    db.receipts.hasMany(db.purchase_invoices, {
      as: 'purchase_invoices_receipt',
      foreignKey: {
          name: 'receiptId',
      },
      constraints: false,
    });



    db.receipts.hasMany(db.expenses, {
      as: 'expenses_receipt',
      foreignKey: {
          name: 'receiptId',
      },
      constraints: false,
    });









//end loop



    db.receipts.belongsTo(db.merchants, {
      as: 'merchant',
      foreignKey: {
        name: 'merchantId',
      },
      constraints: false,
    });

    db.receipts.belongsTo(db.users, {
      as: 'uploaded_by',
      foreignKey: {
        name: 'uploaded_byId',
      },
      constraints: false,
    });

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



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

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


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

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



  return receipts;
};


