<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\User_SAML\Listener;

use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Config\IUserConfig;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ISession;
use OCP\IUserSession;
use OCP\Util;

/** @template-implements IEventListener<BeforeTemplateRenderedEvent|Event> */
class LoadAdditionalScriptsListener implements IEventListener {
	public function __construct(
		private readonly ISession $session,
		private readonly IUserSession $userSession,
		private readonly IUserConfig $userConfig,
	) {
	}

	#[\Override]
	public function handle(Event $event): void {
		if (!$event instanceof BeforeTemplateRenderedEvent) {
			return;
		}

		if (!$event->isLoggedIn()) {
			return;
		}

		$user = $this->userSession->getUser();
		if ($user === null) {
			return; // already checked by $event->isLoggedIn above
		}
		$timezoneDB = $this->userConfig->getValueString($user->getUID(), 'core', 'timezone');

		if ($timezoneDB === '' || !$this->session->exists('timezone')) {
			Util::addScript('user_saml', 'user_saml-timezone');
		}
	}
}
