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

name: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"closed"

        ],

      },

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

      },

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

  fiscal_years.associate = (db) => {


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
















    db.fiscal_years.hasMany(db.budget_programs, {
      as: 'budget_programs_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });




    db.fiscal_years.hasMany(db.budget_reallocations, {
      as: 'budget_reallocations_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });


    db.fiscal_years.hasMany(db.procurement_plans, {
      as: 'procurement_plans_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });









    db.fiscal_years.hasMany(db.programs, {
      as: 'programs_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });





















    db.fiscal_years.hasMany(db.ledger_entries, {
      as: 'ledger_entries_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });





//end loop



    db.fiscal_years.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return fiscal_years;
};


