<?php

namespace App\Models\OrderManagement\Scopes;

use App\Models\OrderManagement\BillingDesk;
use App\Models\UserManagement\Customer;
use App\Models\UserManagement\UserType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class OrderGlobalScope implements Scope{


    /** Apply the scope to a given Eloquent query builder.
     * @param Builder $builder
     * @param Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model){
        $user = auth(activeGuard())->user();
        $search = request()->search['value'] ?? null;
        $customer_table = (new Customer)->getTable();
        $point_of_sale_table = (new BillingDesk)->getTable();
        $columns_search_customer = collect(['first_name',"last_name","phone","email"])->map(fn ($column) => "$customer_table.$column")->toArray();

       // dd($columns_search_customer);
        $builder->when(filled($user),
                    fn ($query) => $query->when($user->isCustomer() ,
                                                fn($q) => $q->whereContains('customer_id',$user->id)
                                            )
                                            ->when($user->isAdmin() ,
                                                fn($q) => $q->whereContains('billing_desk_id',$user->billingDesks->pluck('id')->toArray())
                                            )
                )
//                ->when(auth(activeGuard())->user()->isAdmin(),function ($query){
//                    $point_of_sales = auth(activeGuard())->user()->pointOfSales->pluck('id');
//                    return $query->whereIn('point_of_sale_id',$point_of_sales);
//                })
                ->when(request()->filled("statuses"),function ($query){
                    return $query->whereContains('status_code',request()->get("statuses"));
                })
                ->when(request()->filled("billing_desks"),function ($query){
                    return $query->whereContains('billing_desk_id',request()->get("billing_desks"));
                })
                ->when(request()->filled("users"),function ($query){
                    return $query->whereContains('created_by',request()->get("users"));
                })
                ->when(request()->filled("start_date") and request()->filled("end_date"),function ($query){
                    return $query->whereDateBetween('created_at',request()->get("start_date"),request()->get("end_date"));
                })
                ->when(filled($search),fn ($query) => $query->orWhere(fn($query) => $query->whereLike("ref_id",$search)
                                                                                        ->orWhere(fn ($q) => $q->whereHas('customer',
                                                                                            fn ($q)=>$q->whereLike($columns_search_customer,$search) )
                                                                                        )
                                                             )
               )
               ;
    }
}
