<?php

namespace App\Components\Admin\Users;

use App\Models\User\User;
use Livewire\Component;

class View extends Component {
    public User $user;
    public string $activeTab = 'profile';
//    protected $queryString = ['activeTab' => ['except' => 'profile', 'as' => 'tab']];

    public function mount() {
        $this->checkTabAccess();
    }

    protected function checkTabAccess() {
        if ($this->activeTab === 'api_keys' && auth()->user()->cannot('manageApiKeys', $this->user)) {
            $this->activeTab = 'profile';
        }
        if ($this->activeTab === 'sessions' && auth()->user()->cannot('manageSessions', $this->user)) {
            $this->activeTab = 'profile';
        }
        if ($this->activeTab === 'history' && auth()->user()->cannot('viewHistory', $this->user)) {
            $this->activeTab = 'profile';
        }
    }

    public function updatedActiveTab() {
        $this->checkTabAccess();
    }

    public function setTab(string $tab) {
        $this->activeTab = $tab;
        $this->checkTabAccess();
    }

    public function render() {
        return view('components.admin.users.view');
    }
}
