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

pickup_at: {
        type: DataTypes.DATE,
      
      

      },

dropoff_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"pending_payment",


"confirmed",


"checked_out",


"checked_in",


"cancelled",


"no_show"

        ],

      },

payment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unpaid",


"authorized",


"paid",


"partially_refunded",


"refunded",


"failed"

        ],

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

extras_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

insurance_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

customer_notes: {
        type: DataTypes.TEXT,
      
      

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  bookings.associate = (db) => {


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
















    db.bookings.hasMany(db.booking_extras, {
      as: 'booking_extras_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


    db.bookings.hasMany(db.payments, {
      as: 'payments_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


    db.bookings.hasMany(db.invoices, {
      as: 'invoices_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


    db.bookings.hasMany(db.messages, {
      as: 'messages_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });



    db.bookings.hasMany(db.tasks, {
      as: 'tasks_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });






//end loop



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

    db.bookings.belongsTo(db.users, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

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

    db.bookings.belongsTo(db.branches, {
      as: 'pickup_branch',
      foreignKey: {
        name: 'pickup_branchId',
      },
      constraints: false,
    });

    db.bookings.belongsTo(db.branches, {
      as: 'dropoff_branch',
      foreignKey: {
        name: 'dropoff_branchId',
      },
      constraints: false,
    });

    db.bookings.belongsTo(db.rate_plans, {
      as: 'rate_plan',
      foreignKey: {
        name: 'rate_planId',
      },
      constraints: false,
    });

    db.bookings.belongsTo(db.insurance_plans, {
      as: 'insurance_plan',
      foreignKey: {
        name: 'insurance_planId',
      },
      constraints: false,
    });

    db.bookings.belongsTo(db.promo_codes, {
      as: 'promo_code',
      foreignKey: {
        name: 'promo_codeId',
      },
      constraints: false,
    });

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




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

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



  return bookings;
};


