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

agreement_number: {
        type: DataTypes.TEXT,
      
      

      },

agreement_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"rental",


"lease"

        ],

      },

billing_frequency: {
        type: DataTypes.ENUM,
      
      

        values: [

"daily",


"weekly",


"monthly"

        ],

      },

daily_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

weekly_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

monthly_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

deposit_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

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

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"paused",


"ended",


"cancelled"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  agreements.associate = (db) => {


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









    db.agreements.hasMany(db.recurring_invoice_templates, {
      as: 'recurring_invoice_templates_agreement',
      foreignKey: {
          name: 'agreementId',
      },
      constraints: false,
    });


    db.agreements.hasMany(db.invoices, {
      as: 'invoices_agreement',
      foreignKey: {
          name: 'agreementId',
      },
      constraints: false,
    });








    db.agreements.hasMany(db.driver_expenses, {
      as: 'driver_expenses_agreement',
      foreignKey: {
          name: 'agreementId',
      },
      constraints: false,
    });











//end loop



    db.agreements.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.agreements.belongsTo(db.drivers, {
      as: 'driver',
      foreignKey: {
        name: 'driverId',
      },
      constraints: false,
    });

    db.agreements.belongsTo(db.vehicles, {
      as: 'vehicle',
      foreignKey: {
        name: 'vehicleId',
      },
      constraints: false,
    });




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

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



  return agreements;
};


