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

business_name: {
        type: DataTypes.TEXT,
      
      

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

tin_number: {
        type: DataTypes.TEXT,
      
      

      },

vat_registration_number: {
        type: DataTypes.TEXT,
      
      

      },

taxpayer_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"vat",


"tot",


"both",


"income_tax_only"

        ],

      },

industry: {
        type: DataTypes.ENUM,
      
      

        values: [

"retail",


"wholesale",


"services",


"manufacturing",


"hospitality",


"other"

        ],

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

onboarded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  merchants.associate = (db) => {


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








    db.merchants.hasMany(db.customers, {
      as: 'customers_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.suppliers, {
      as: 'suppliers_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.products, {
      as: 'products_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


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


    db.merchants.hasMany(db.ocr_extractions, {
      as: 'ocr_extractions_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.sales_invoices, {
      as: 'sales_invoices_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });



    db.merchants.hasMany(db.purchase_invoices, {
      as: 'purchase_invoices_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });



    db.merchants.hasMany(db.expenses, {
      as: 'expenses_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.tax_periods, {
      as: 'tax_periods_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.vat_declarations, {
      as: 'vat_declarations_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.tot_declarations, {
      as: 'tot_declarations_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.declaration_submissions, {
      as: 'declaration_submissions_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.messages, {
      as: 'messages_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });


    db.merchants.hasMany(db.audit_events, {
      as: 'audit_events_merchant',
      foreignKey: {
          name: 'merchantId',
      },
      constraints: false,
    });



//end loop



    db.merchants.belongsTo(db.firms, {
      as: 'firm',
      foreignKey: {
        name: 'firmId',
      },
      constraints: false,
    });

    db.merchants.belongsTo(db.users, {
      as: 'assigned_accountant',
      foreignKey: {
        name: 'assigned_accountantId',
      },
      constraints: false,
    });

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




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

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



  return merchants;
};


