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

      ref: {
        type: DataTypes.TEXT,
      },

      status_manual: {
        type: DataTypes.ENUM,

        values: ['en_attente', 'confirmee', 'annulee'],
      },

      status_computed: {
        type: DataTypes.ENUM,

        values: [
          'a_venir',

          'en_cours',

          'a_restituer',

          'en_retard',

          'terminee',

          'annulee',

          'no_show',
        ],
      },

      start_at: {
        type: DataTypes.DATE,
      },

      end_at: {
        type: DataTypes.DATE,
      },

      pickup_address: {
        type: DataTypes.TEXT,
      },

      return_address: {
        type: DataTypes.TEXT,
      },

      options: {
        type: DataTypes.TEXT,
      },

      days: {
        type: DataTypes.INTEGER,
      },

      price_per_day: {
        type: DataTypes.DECIMAL,
      },

      total_price: {
        type: DataTypes.DECIMAL,
      },

      paid_amount: {
        type: DataTypes.DECIMAL,
      },

      balance_amount: {
        type: DataTypes.DECIMAL,
      },

      guarantee_method: {
        type: DataTypes.ENUM,

        values: ['especes', 'carte_bloquee', 'cheque', 'aucune'],
      },

      guarantee_amount: {
        type: DataTypes.DECIMAL,
      },

      guarantee_status: {
        type: DataTypes.ENUM,

        values: ['retenue', 'restituee', 'partielle', 'non_applicable'],
      },

      departure_validated_at: {
        type: DataTypes.DATE,
      },

      return_validated_at: {
        type: DataTypes.DATE,
      },

      notes: {
        type: DataTypes.TEXT,
      },

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

  reservations.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.reservations.hasMany(db.claims, {
      as: 'claims_reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.reservations.hasMany(db.drivers, {
      as: 'drivers_reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.reservations.hasMany(db.guarantees, {
      as: 'guarantees_reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.reservations.hasMany(db.infractions, {
      as: 'infractions_reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.reservations.hasMany(db.payments, {
      as: 'payments_reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.reservations.hasMany(db.vehicle_conditions, {
      as: 'vehicle_conditions_reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    //end loop

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

    db.reservations.belongsTo(db.clients, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

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

    db.reservations.belongsTo(db.agences, {
      as: 'agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

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

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

  return reservations;
};
