<?php

namespace App\Livewire;

use App\Models\User;

use Livewire\Component;
use Livewire\WithPagination;
use Livewire\Attributes\Url;

class AdminUserSearch extends Component
{
    use WithPagination;

    #[Url(history: true)]
    public $search = '';

    #[Url(history: true)]
    public $filtered = ['true'];

    #[Url(history: true)]
    public $patreon = [];

    #[Url(history: true)]
    public $banned = [];

    public function render()
    {
        $users = User::when($this->filtered !== [], fn ($query) => $query->where('id', '>=', 10000))
            ->when($this->patreon !== [], fn ($query) => $query->where('is_patreon', 1))
            ->when($this->banned !== [], fn ($query) => $query->where('is_banned', 1))
            ->when($this->search !== '',  fn ($query) => $query->where(function($query) { 
                $query->where('username', 'like', '%'.$this->search.'%')
                    ->orWhere('global_name', 'like', '%'.$this->search.'%'); 
                }))
            ->paginate(20);

        return view('livewire.admin-user-search', [
            'users' => $users
        ]);
    }
}
