<?php

declare(strict_types=1);

namespace App\Policies\Concerns;

use App\Models\User;

trait ScopedByDepartment
{
    protected function userManagesDepartment(User $user, ?int $departmentId): bool
    {
        if (! $user->hasRole('department_manager')) {
            return true;
        }

        if ($departmentId === null) {
            return false;
        }

        return $user->department_id === $departmentId;
    }

    protected function userIsViewer(User $user): bool
    {
        return $user->hasRole('viewer');
    }

    protected function isSuperAdmin(User $user): bool
    {
        return $user->hasRole('super_admin');
    }

    protected function hasFullAccess(User $user): bool
    {
        return $user->hasRole(['administrator', 'hr_manager']);
    }

    protected function canForceDelete(User $user): bool
    {
        return $user->hasRole('administrator');
    }

    protected function canRead(User $user): bool
    {
        return $this->hasFullAccess($user)
            || $user->hasRole(['supervisor', 'department_manager'])
            || $this->userIsViewer($user);
    }
}
