<?php

namespace Planaday\Html\NodeTags\Course\Details\Page;

use Planaday\Config\Course\Detail\DetailSettings;
use Planaday\Config\Course\General\GeneralSettings;
use Planaday\Config\General\Price\PriceSettings;
use Planaday\Entities\Course\CourseUtility;
use Planaday\Entities\Course\DTO\Course;
use Planaday\Entities\DayPart\DayPartController;
use Planaday\Html\Nodes\NodeData;
use Planaday\Html\NodeTags\DayPart\ListItem\DayPartListItemSettings;
use Planaday\Html\NodeTags\Tag;
use Planaday\Html\NodeTags\TemplateOnlyTag;
use Planaday\Html\Parsers\NodeParsers\TagMarkers\TestDataTag;
use Planaday\Utility\DependencyInjection\Attributes\Transient;

#[Transient]
class Page implements Tag, TestDataTag {
	use TemplateOnlyTag;

	public function __construct(
		protected DayPartController $dayPartController,
		protected CourseUtility $courseUtility,
		protected DetailPageSettings $detailPageSettings,
		protected GeneralSettings $generalSettings,
		protected PriceSettings $priceSettings,
		protected DayPartListItemSettings $dayPartListItemSettings
	) {
		$this->config = [
			"dayPartText"               => $this->generalSettings->getDayPartText( ... ),
			"countOptions"              => $this->generalSettings->isIncludeOptionInCount( ... ),
			"showAvailableSpots"        => $this->detailPageSettings->isShowAvailability( ... ),
			"showIcon"                  => $this->detailPageSettings->isShowIcons( ... ),
			"showPrice"                 => $this->detailPageSettings->isShowPrice( ... ),
			"showLevel"                 => $this->detailPageSettings->isShowLevelCourse( ... ),
			"showLabels"                => $this->detailPageSettings->isShowLabels( ... ),
			"showDescription"           => $this->detailPageSettings->isShowDescription( ... ),
			"withTax"                   => $this->priceSettings->isIncludingTax( ... ),
			"showBothWithAndWithoutTax" => $this->detailPageSettings->isShowPriceWithAndWithoutTax( ... ),
			"showDayPartsDetail"        => $this->detailPageSettings->isShowDayPartCount( ... ),
			"showCourseElearning"       => $this->detailPageSettings->isShowELearningIcon( ... ),
			"showGaranteedStart"        => $this->detailPageSettings->isShowStartGuaranteeLabel( ... ),
			"showMoneyBack"             => $this->detailPageSettings->isShowMoneyBackLabel( ... ),
			"showSoobDetail"            => $this->detailPageSettings->isShowSoobLabel( ... ),
			"showCode95"                => $this->detailPageSettings->isShowCode95Label( ... ),
			"showCostRemark"            => $this->detailPageSettings->isShowFinancialInfo( ... ),
			"showDayPartList"           => $this->dayPartListItemSettings->isShowList( ... ),
			"includeELearning"          => $this->generalSettings->isIncludeELearningDayParts( ... ),
		];
	}

	protected function prepareData( NodeData $nodeData ): void {
		$course = $this->attributes["course"] ?? $this->nodeData->data["course"] ?? null;

		if ( ! $course instanceof Course ) {
			return;
		}

		$nodeData->data["hasPastDayparts"] = fn () => $course->hasPastDayParts($this->generalSettings->isELearningCountsAsStart());

		$nodeData->data['availableSpots'] = function () use ( $course ) {
			return $course->getUsersData()->getAvailableSpots( $this->generalSettings->isIncludeOptionInCount() );
		};

		$nodeData->data['dayPartCount'] ??= function () use ( $course ) {
			return $this->courseUtility->getDayPartCount( $course, $this->generalSettings->isIncludeELearningDayParts() );
		};
	}

	public function getTagDir(): string {
		return __DIR__;
	}

	public function injectTestData(): void {
		$this->nodeData->data['course'] ??= new Course( 1 );

		$this->nodeData->data['dayPartCount'] = function () {
			return 1 + $this->generalSettings->isIncludeELearningDayParts();
		};
	}
}