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

name: {
        type: DataTypes.TEXT,
      
      

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closing",


"closed"

        ],

      },

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

  fiscal_periods.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_periods.hasMany(db.journal_entries, {
      as: 'journal_entries_fiscal_period',
      foreignKey: {
          name: 'fiscal_periodId',
      },
      constraints: false,
    });














//end loop



    db.fiscal_periods.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return fiscal_periods;
};


