<?php

namespace AutoAdmin\Bundle\Admin\Helper;

use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use Symfony\Bundle\SecurityBundle\Security;

class UserTimezoneProvider
{
    public function __construct(
        private readonly Security $security,
        private readonly SiteContextHelper $siteContextHelper
    )
    {
    }

    public function getUserTimezone(): string
    {
        $user = $this->security->getUser();

        if ($user && method_exists($user, 'getTimezone')) {
            $userTimezone = $user->getTimezone();
            if (!empty($userTimezone)) {
                return $userTimezone;
            }
        }

        return $this->siteContextHelper->getLocale()?->getTimezone();
    }
}
