<?php

namespace App\Policies;

use App\Enums\RoleName;
use App\Models\User;

class UserPolicy
{
    public function viewAny(User $actor): bool
    {
        return $actor->isActive() && $actor->hasPermission('users.view');
    }

    public function view(User $actor, User $subject): bool
    {
        return $this->viewAny($actor);
    }

    public function create(User $actor): bool
    {
        return $actor->isActive() && $actor->hasPermission('users.manage');
    }

    public function update(User $actor, User $subject): bool
    {
        if (! $actor->isActive() || ! $actor->hasPermission('users.manage')) {
            return false;
        }
        if ($subject->hasRole(RoleName::SuperAdmin) && ! $actor->hasRole(RoleName::SuperAdmin)) {
            return false;
        }

        return $actor->isNot($subject) || $actor->hasRole(RoleName::SuperAdmin);
    }

    public function delete(User $actor, User $subject): bool
    {
        if (! $this->update($actor, $subject) || $actor->is($subject)) {
            return false;
        }

        return true;
    }
}
