<?php

namespace App\Services;

final class SchoolYearAccessPolicy
{
    public function canView(?int $userId, array $year): bool
    {
        if (($year['status'] ?? '') !== 'draft') {
            return true;
        }

        return $this->canViewDraft($userId);
    }

    public function canSelect(?int $userId, array $year): bool
    {
        return $this->canView($userId, $year);
    }

    public function canViewDraft(?int $userId): bool
    {
        if ($userId === null || $userId <= 0) {
            return false;
        }

        $roles = array_map(
            static fn ($role): string => strtolower(trim((string) $role)),
            (array) session()->get('roles')
        );

        return (bool) array_intersect($roles, [
            'admin',
            'administrator',
            'administrative staff',
            'principal',
        ]);
    }
}
