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,
      },

level_number: {
        type: DataTypes.INTEGER,
      
      

      },

level_name: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

platform_fee: {
        type: DataTypes.DECIMAL,
      
      

      },

tutor_payout: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending_acceptance",


"active",


"milestone_submitted",


"revision_requested",


"completed",


"disputed",


"cancelled",


"refunded"

        ],

      },

escrow_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"held",


"released",


"refunded"

        ],

      },

scheduled_at: {
        type: DataTypes.DATE,
      
      

      },

accepted_at: {
        type: DataTypes.DATE,
      
      

      },

milestone_submitted_at: {
        type: DataTypes.DATE,
      
      

      },

auto_release_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

deliverable_notes: {
        type: DataTypes.TEXT,
      
      

      },

revision_count: {
        type: DataTypes.INTEGER,
      
      

      },

revision_reason: {
        type: DataTypes.TEXT,
      
      

      },

jitsi_meet_meeting_id: {
        type: DataTypes.TEXT,
      
      

      },

jitsi_meet_password: {
        type: DataTypes.TEXT,
      
      

      },

admin_note: {
        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.reviews, {
      as: 'reviews_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


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


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

























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



//end loop



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

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

    db.bookings.belongsTo(db.gigs, {
      as: 'gig',
      foreignKey: {
        name: 'gigId',
      },
      constraints: false,
    });

    db.bookings.belongsTo(db.gig_levels, {
      as: 'gig_level',
      foreignKey: {
        name: 'gig_levelId',
      },
      constraints: false,
    });



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


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

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



  return bookings;
};


