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

bill_number: {
        type: DataTypes.TEXT,
      
      

      },

period_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"bulanan",


"sekali"

        ],

      },

period_year: {
        type: DataTypes.INTEGER,
      
      

      },

period_month: {
        type: DataTypes.INTEGER,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

amount_total: {
        type: DataTypes.DECIMAL,
      
      

      },

amount_paid: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"belum_bayar",


"cicilan",


"lunas",


"jatuh_tempo",


"dibatalkan"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  billing_items.associate = (db) => {


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



























    db.billing_items.hasMany(db.payments, {
      as: 'payments_billing_item',
      foreignKey: {
          name: 'billing_itemId',
      },
      constraints: false,
    });




//end loop



    db.billing_items.belongsTo(db.fee_definitions, {
      as: 'fee_definition',
      foreignKey: {
        name: 'fee_definitionId',
      },
      constraints: false,
    });

    db.billing_items.belongsTo(db.students, {
      as: 'student',
      foreignKey: {
        name: 'studentId',
      },
      constraints: false,
    });

    db.billing_items.belongsTo(db.schools, {
      as: 'schools',
      foreignKey: {
        name: 'schoolsId',
      },
      constraints: false,
    });




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

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



  return billing_items;
};


