<?php

namespace Planaday\Html\NodeTags\Testing\PersonalTesting;

use DOMNode;
use Planaday\Config\Payment\PaymentSettings;
use Planaday\Html\Nodes\ParserNode;
use Planaday\Html\NodeTags\InnerContentControl;
use Planaday\Html\NodeTags\Tag;
use Planaday\Html\NodeTags\TagCore;
use Planaday\Html\NodeTags\TagNotFullyInitializedException;
use Planaday\Platform\PlatformAbstraction\DataHandler;
use Planaday\Utility\DependencyInjection\Attributes\Transient;
use Planaday\Utility\FormBuilder\Form;
use Planaday\Utility\FormBuilder\SubmitStrategies\RestSubmit;
use Planaday\Utility\PluginData;

#[Transient]
class PersonalTesting implements Tag {
	use TagCore;
	use InnerContentControl;

	public function __construct(
		protected PaymentSettings $paymentSettings,
		protected DataHandler $dataHandler,
		protected PluginData $pluginData,
	) {
	}

	public function getNewNode(): DOMNode&ParserNode {
		if ( ! isset( $this->originalNode ) ) {
			throw new TagNotFullyInitializedException( "The OriginalNode is not set" );
		}

		$form = new Form();

		$form->setName( "testForm" )
			 ->setSubmitBehaviour(
				 (new RestSubmit())
					 ->setHeaders($this->dataHandler->getRestNonceHeader())
					 ->setUrl('/wp-json/' . $this->pluginData->getFullPrefix() . '/v1/settings/')
			 )
			 ->setName('settings')
			 ->addContents(
				$this->paymentSettings->getFormGroup()
			 );

		$this->nodeData->data["formJson"] = json_encode($form);

		$node = $this->originalNode->getNodeFromTemplate( $this->getTemplate( '', 'test' ) );
		$node->setExtendedNodeData( $this->nodeData );
		return $node;
	}

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