<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\User;
use App\Policies\Concerns\ScopedByDepartment;

class UserPolicy
{
    use ScopedByDepartment;

    public function viewAny(User $user): bool
    {
        return $this->isSuperAdmin($user) || $user->hasAnyRole(['administrator', 'hr_manager']);
    }

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

    public function create(User $user): bool
    {
        return $this->isSuperAdmin($user) || $user->hasAnyRole(['administrator', 'hr_manager']);
    }

    public function update(User $user, User $model): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        if ($model->hasRole('super_admin')) {
            return false;
        }

        return $user->hasAnyRole(['administrator', 'hr_manager']);
    }

    public function delete(User $user, User $model): bool
    {
        if ($model->hasRole('super_admin') || $user->id === $model->id) {
            return false;
        }

        return $this->isSuperAdmin($user) || $user->hasRole('administrator');
    }

    public function restore(User $user, User $model): bool
    {
        return $this->delete($user, $model);
    }

    public function forceDelete(User $user, User $model): bool
    {
        return $this->isSuperAdmin($user);
    }
}
