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

lease_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"ended",


"terminated"

        ],

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

rent_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

billing_cycle: {
        type: DataTypes.ENUM,
      
      

        values: [

"daily",


"weekly",


"monthly",


"quarterly",


"yearly"

        ],

      },

deposit_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

signed_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  leases.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.leases.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.leases.belongsTo(db.assets, {
      as: 'asset',
      foreignKey: {
        name: 'assetId',
      },
      constraints: false,
    });

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



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


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

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



  return leases;
};


