<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\Profile;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Throwable;

final class ActorProfileResolver
{
    public function __construct(
        private readonly RequestStack $requestStack,
        private readonly Security $security,
    ) {}

    public function resolve(): ?string
    {
        try {
            $session = $this->requestStack->getSession();
            if ($session instanceof SessionInterface) {
                $profileId = $session->get('profileId');
                if ($profileId) {
                    return (string) $profileId;
                }
            }
        } catch (Throwable) {
            // No session available (CLI context, etc.)
        }

        $user = $this->security->getUser();
        if ($user instanceof Profile) {
            return $user->getId();
        }

        return null;
    }
}
