<?php

namespace App\Classes\General\Eloquent\Filters;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;

class HasUsedVoucherForAdmin implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return mixed
     */
    public static function apply(Builder $builder, $value)
    {
        $userId = $value !== 1 ? $value : Auth::user()->id;

        return $builder->where('user_id', $userId)
        ->where(function ($query) use ($userId){
            $query->whereHas('voucher', function ($subquery) use ($userId){
                $subquery->whereHas('redemptions', function ($subsubquery) use ($userId){
                    $subsubquery->whereHas('transaction', function ($subsubsubquery) use ($userId){
                        $subsubsubquery->whereHas('booking', function ($s4query) use ($userId){
                            $s4query->whereHas('company', function ($s5query) use ($userId) {
                                $s5query->whereHas('employees', function ($s6query) use ($userId){
                                    $s6query->where('user_id', $userId);
                                });
                            });
                        });
                    });
                });
            });
        });
    }
}
