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

base_year: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"rascunho",


"em_preenchimento",


"aguardando_revisao",


"concluida",


"gerada",


"enviada",


"retificada"

        ],

      },

tax_model: {
        type: DataTypes.ENUM,
      
      

        values: [

"simplificado",


"completo"

        ],

      },

declaration_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"original",


"retificadora"

        ],

      },

previous_receipt_number: {
        type: DataTypes.TEXT,
      
      

      },

tax_calculated: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_withheld: {
        type: DataTypes.DECIMAL,
      
      

      },

refund_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

payable_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

risk_score: {
        type: DataTypes.INTEGER,
      
      

      },

risk_details_json: {
        type: DataTypes.TEXT,
      
      

      },

dec_file_url: {
        type: DataTypes.TEXT,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

last_calculated_at: {
        type: DataTypes.DATE,
      
      

      },

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

  declarations.associate = (db) => {


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









    db.declarations.hasMany(db.dependents, {
      as: 'dependents_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.data_sources, {
      as: 'data_sources_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.transactions, {
      as: 'transactions_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.documents, {
      as: 'documents_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.income_payers_pj, {
      as: 'income_payers_pj_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.income_carne_leao_monthly, {
      as: 'income_carne_leao_monthly_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.exempt_incomes, {
      as: 'exempt_incomes_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.exclusive_incomes, {
      as: 'exclusive_incomes_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.assets, {
      as: 'assets_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.debts, {
      as: 'debts_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.deductible_payments, {
      as: 'deductible_payments_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.investment_positions, {
      as: 'investment_positions_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.trades, {
      as: 'trades_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.monthly_capital_gains, {
      as: 'monthly_capital_gains_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });


    db.declarations.hasMany(db.risk_analyses, {
      as: 'risk_analyses_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });







    db.declarations.hasMany(db.job_runs, {
      as: 'job_runs_declaration',
      foreignKey: {
          name: 'declarationId',
      },
      constraints: false,
    });



//end loop



    db.declarations.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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




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

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



  return declarations;
};


