<?php

namespace App\Models\Auth;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

use App\Models\Auth\UserRole;
use App\Models\Auth\UserPermission;
use App\Models\Esi\EsiScope;
use App\Models\Esi\EsiToken;
use App\Models\SRP\Ships as SRPShips;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'character_owner_hash',
        'character_name',
        'character_id',
        'avatar',
        'token',
        'refresh_token',
        'expiresIn',
        'user_jwt', // holds jwt (per spec)
        'user_jwt_issued_at',
        'user_jwt_expires_at',
        'privleges_version',
    ];

    protected $hidden = [
        'token',
        'refresh_token',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'expiresIn' => 'integer',
            'privileges_version' => 'integer',
            'user_jwt_issued_at' => 'datetime',
            'user_jwt_expires_at' => 'datetime',
        ];
    }

    public function jwtNeedsRefresh(int $refreshIntervalSeconds = 3600): bool
    {
        if (blank($this->user_jwt) || ! $this->user_jwt_issued_at) {
            return true;
        }

        return $this->user_jwt_issued_at->lte(now()->subSeconds($refreshIntervalSeconds));
    }

    public function markPrivilegesChanged(): void 
    {
        $this->increment('privileges_version');
    }

    public function getName() {
        return $this->character_name;
    }

    public function getId() {
        return $this->character_id;
    }

    public function getRole() {
        $role = UserRole::where([
            'character_id' => $this->character_id,
        ])->first();

        return $role->role;
    }

    public function hasRole($role) {
        //If the user is a super use then he has all roles
        if($this->hasSuperUser()) {
            return true;
        }

        $found = UserRole::where(['character_id' => $this->character_id, 'role' => $role])->get(['role']);

        if(isset($found[0]) && $found[0]->role == $role) {
            return true;
        } else {
            return false;
        }
    }

    public function hasSuperUser() {
        $found = UserRole::where(['character_id' => $this->character_id])->first();
        if(isset($found[0]->role) && $found[0]->role == 'SuperUser') {
            return true;
        } else {
            return false;
        }
    }

    public function srpOpen() {
        return SRPShips::where([
            'character_id' => $this->character_id,
            'approval' => 'Under Review',
        ])->count();
    }

    public function srpDenied() {
        return SRPShips::where([
            'character_id' => $this->character_id,
            'approval' => 'Denied',
        ])->count();
    }

    public function srpApproved() {
        return SRPShips::where([
            'character_id' => $this->character_id,
            'approval' => 'Approved',
        ])->count();
    }

    public function getAvatar() {
        return $this->avatar;
    }
}
