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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"generated",


"reviewed",


"submitted",


"accepted",


"rejected",


"amended"

        ],

      },

gross_sales_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tot_rate_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

tot_payable_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

generated_at: {
        type: DataTypes.DATE,
      
      

      },

reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  tot_declarations.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.tot_declarations.belongsTo(db.merchants, {
      as: 'merchant',
      foreignKey: {
        name: 'merchantId',
      },
      constraints: false,
    });

    db.tot_declarations.belongsTo(db.tax_periods, {
      as: 'tax_period',
      foreignKey: {
        name: 'tax_periodId',
      },
      constraints: false,
    });

    db.tot_declarations.belongsTo(db.users, {
      as: 'generated_by',
      foreignKey: {
        name: 'generated_byId',
      },
      constraints: false,
    });

    db.tot_declarations.belongsTo(db.users, {
      as: 'reviewed_by',
      foreignKey: {
        name: 'reviewed_byId',
      },
      constraints: false,
    });

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



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

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

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


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

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



  return tot_declarations;
};


