<?php
/**
 * Renders the statement model into semantic, escaped HTML.
 *
 * The output is a DISCLOSURE document. It cites the legal basis, states the
 * self-declared conformance status, lists known limitations in three buckets,
 * gives the feedback route and the national enforcement body, and always ends
 * with a non-removable honest disclaimer. It NEVER emits a literal claim that
 * the website "is compliant".
 */

defined( 'ABSPATH' ) || exit;

class TSFFE_Render {

	/**
	 * @param array<string, mixed> $data       A sanitized statement model (see TSFFE_Data).
	 * @param bool                 $standalone True = embedded/exported: include the
	 *                             "Accessibility Statement" heading (sections h3).
	 *                             False = shown on the dedicated statement page whose
	 *                             own title is already that heading: omit it and
	 *                             promote sections to h2 so no level is skipped.
	 */
	public static function html( array $data, bool $standalone = true ): string {
		$data = array_merge( TSFFE_Data::defaults(), $data );

		$wcag        = $data['wcag_version'] ? $data['wcag_version'] : '2.1';
		$conformance = $data['conformance'] ? $data['conformance'] : TSFFE_Data::CONFORMANCE_PARTIAL;
		$body        = TSFFE_Bodies::get( (string) $data['enforcement_country'] );

		$org  = $data['org_name'] ? $data['org_name'] : ( $data['site_name'] ? $data['site_name'] : '' );
		$site = $data['site_url'];

		$hs = $standalone ? 'h3' : 'h2'; // section heading level
		$hb = $standalone ? 'h4' : 'h3'; // limitation-bucket heading level

		ob_start();
		?>
		<div class="tsffe-statement">
			<?php if ( $standalone ) : ?>
				<h2><?php esc_html_e( 'Accessibility Statement', 'tendforge-statement-forge-for-eaa' ); ?></h2>
			<?php endif; ?>

			<?php /* --- Commitment --- */ ?>
			<section class="tsffe-section tsffe-commitment">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Our commitment', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<p>
					<?php
					if ( $org ) {
						printf(
							/* translators: 1: organisation name, 2: website address */
							esc_html__( '%1$s is committed to making %2$s as accessible as we reasonably can, in line with the European Accessibility Act and the EU Web Accessibility Directive.', 'tendforge-statement-forge-for-eaa' ),
							esc_html( $org ),
							$site ? '<a href="' . esc_url( $site ) . '">' . esc_html( $site ) . '</a>' : esc_html__( 'this website', 'tendforge-statement-forge-for-eaa' )
						);
					} else {
						esc_html_e( 'We are committed to making this website as accessible as we reasonably can, in line with the European Accessibility Act and the EU Web Accessibility Directive.', 'tendforge-statement-forge-for-eaa' );
					}
					?>
				</p>
			</section>

			<?php /* --- Scope --- */ ?>
			<section class="tsffe-section tsffe-scope">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Scope of this statement', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<p>
					<?php
					if ( $data['scope'] ) {
						echo esc_html( $data['scope'] );
					} elseif ( $site ) {
						printf(
							/* translators: %s: website address */
							esc_html__( 'This statement applies to the website published at %s.', 'tendforge-statement-forge-for-eaa' ),
							'<a href="' . esc_url( $site ) . '">' . esc_html( $site ) . '</a>'
						);
					} else {
						esc_html_e( 'This statement applies to this website.', 'tendforge-statement-forge-for-eaa' );
					}
					?>
				</p>
			</section>

			<?php /* --- Conformance status + legal basis --- */ ?>
			<section class="tsffe-section tsffe-conformance">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Conformance status', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<p class="tsffe-conformance-status">
					<strong><?php echo esc_html( TSFFE_Data::conformance_label( $conformance, $wcag ) ); ?></strong>
				</p>
				<p>
					<?php
					printf(
						/* translators: 1: EN standard reference incl. WCAG version e.g. "EN 301 549 V3.2.1 (which incorporates WCAG 2.1 AA)" */
						esc_html__( 'The accessibility requirements applied are those of %1$s. This is a self-declared assessment by the site owner.', 'tendforge-statement-forge-for-eaa' ),
						'<strong>' . esc_html( sprintf( 'EN 301 549 V3.2.1 (which incorporates WCAG %s AA)', $wcag ) ) . '</strong>'
					);
					?>
				</p>
				<p>
					<?php
					if ( TSFFE_Data::CONFORMANCE_PARTIAL === $conformance ) {
						esc_html_e( '"Partially conformant" means that some parts of the content do not yet fully meet the requirements above. The known shortfalls are listed below.', 'tendforge-statement-forge-for-eaa' );
					} elseif ( TSFFE_Data::CONFORMANCE_NONE === $conformance ) {
						esc_html_e( '"Non-conformant" means the content has not yet been assessed against, or does not yet meet, the requirements above.', 'tendforge-statement-forge-for-eaa' );
					} else {
						esc_html_e( 'This is the site owner\'s self-declared assessment and is not an independent certification.', 'tendforge-statement-forge-for-eaa' );
					}
					?>
				</p>
			</section>

			<?php /* --- Non-accessible content: three buckets --- */ ?>
			<section class="tsffe-section tsffe-limitations">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Non-accessible content', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<?php
				$has_any = $data['non_compliance'] || $data['disproportionate_burden'] || $data['out_of_scope'];
				if ( ! $has_any ) :
					?>
					<p><?php esc_html_e( 'No specific limitations have been recorded. This does not guarantee the absence of accessibility barriers; please use the feedback route below to report any you encounter.', 'tendforge-statement-forge-for-eaa' ); ?></p>
					<?php
				endif;

				self::bucket(
					__( 'Non-compliance with the accessibility requirements', 'tendforge-statement-forge-for-eaa' ),
					$data['non_compliance'],
					$hb
				);
				self::bucket(
					__( 'Disproportionate burden', 'tendforge-statement-forge-for-eaa' ),
					$data['disproportionate_burden'],
					$hb
				);
				self::bucket(
					__( 'Content not within the scope of the applicable legislation', 'tendforge-statement-forge-for-eaa' ),
					$data['out_of_scope'],
					$hb
				);
				?>
			</section>

			<?php /* --- Feedback mechanism --- */ ?>
			<section class="tsffe-section tsffe-feedback">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Feedback and contact', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<p><?php esc_html_e( 'We welcome your feedback on the accessibility of this website. Please let us know if you encounter a barrier:', 'tendforge-statement-forge-for-eaa' ); ?></p>
				<?php if ( $data['contact_email'] ) : ?>
					<p>
						<?php esc_html_e( 'Email:', 'tendforge-statement-forge-for-eaa' ); ?>
						<a href="<?php echo esc_attr( 'mailto:' . sanitize_email( $data['contact_email'] ) ); ?>"><?php echo esc_html( $data['contact_email'] ); ?></a>
					</p>
				<?php endif; ?>
				<?php if ( $data['feedback_method'] ) : ?>
					<p><?php echo esc_html( $data['feedback_method'] ); ?></p>
				<?php endif; ?>
				<p><?php esc_html_e( 'We aim to respond to accessibility feedback as soon as we reasonably can.', 'tendforge-statement-forge-for-eaa' ); ?></p>
			</section>

			<?php /* --- Enforcement / complaints body --- */ ?>
			<section class="tsffe-section tsffe-enforcement">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Enforcement procedure', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<?php if ( $body ) : ?>
					<p>
						<?php
						printf(
							/* translators: 1: country name, 2: enforcement body name */
							esc_html__( 'If you are not satisfied with our response, you can contact the designated enforcement body in %1$s: %2$s.', 'tendforge-statement-forge-for-eaa' ),
							esc_html( $body['country'] ),
							'<strong>' . esc_html( $body['body'] ) . '</strong>'
						);
						?>
						<?php if ( ! empty( $body['url'] ) ) : ?>
							<br />
							<a href="<?php echo esc_url( $body['url'] ); ?>"><?php echo esc_html( $body['url'] ); ?></a>
						<?php endif; ?>
					</p>
				<?php else : ?>
					<p><?php esc_html_e( 'If you are not satisfied with our response, you can contact the designated national accessibility enforcement body in your EU member state.', 'tendforge-statement-forge-for-eaa' ); ?></p>
				<?php endif; ?>
			</section>

			<?php /* --- Preparation / review / method --- */ ?>
			<section class="tsffe-section tsffe-meta">
				<<?php echo esc_html( $hs ); ?>><?php esc_html_e( 'Preparation of this statement', 'tendforge-statement-forge-for-eaa' ); ?></<?php echo esc_html( $hs ); ?>>
				<ul>
					<li>
						<?php
						$prepared = $data['prepared_date'] ? $data['prepared_date'] : esc_html__( 'not specified', 'tendforge-statement-forge-for-eaa' );
						printf(
							/* translators: %s: date or "not specified" */
							esc_html__( 'This statement was prepared on: %s', 'tendforge-statement-forge-for-eaa' ),
							esc_html( $prepared )
						);
						?>
					</li>
					<li>
						<?php
						$reviewed = $data['last_reviewed_date'] ? $data['last_reviewed_date'] : esc_html__( 'not specified', 'tendforge-statement-forge-for-eaa' );
						printf(
							/* translators: %s: date or "not specified" */
							esc_html__( 'It was last reviewed on: %s', 'tendforge-statement-forge-for-eaa' ),
							esc_html( $reviewed )
						);
						?>
					</li>
					<li>
						<?php
						$method = ( 'third-party' === $data['assessment_method'] )
							? esc_html__( 'a third-party evaluation', 'tendforge-statement-forge-for-eaa' )
							: esc_html__( 'a self-assessment by the site owner', 'tendforge-statement-forge-for-eaa' );
						printf(
							/* translators: %s: assessment method description */
							esc_html__( 'The assessment was carried out by way of %s.', 'tendforge-statement-forge-for-eaa' ),
							esc_html( $method )
						);
						?>
					</li>
					<?php if ( $data['technologies'] ) : ?>
						<li>
							<?php
							printf(
								/* translators: %s: technologies list */
								esc_html__( 'Accessibility relies on the following technologies: %s', 'tendforge-statement-forge-for-eaa' ),
								esc_html( $data['technologies'] )
							);
							?>
						</li>
					<?php endif; ?>
				</ul>
			</section>

			<?php /* --- Non-removable honest disclaimer footer --- */ ?>
			<footer class="tsffe-section tsffe-disclaimer">
				<p><em><?php esc_html_e( 'This statement is a self-declared disclosure prepared by the site owner. It does not certify compliance and is not legal advice.', 'tendforge-statement-forge-for-eaa' ); ?></em></p>
			</footer>
		</div>
		<?php
		$html = trim( (string) ob_get_clean() );

		/**
		 * Filters the rendered accessibility statement HTML.
		 *
		 * Extension point for add-ons (e.g. an agency white-label footer credit).
		 * Filters MUST NOT alter or remove the legally-required statement text or
		 * the non-removable honest disclaimer, and MUST NOT introduce any bare
		 * "is compliant" claim.
		 *
		 * @param string               $html The rendered statement HTML.
		 * @param array<string, mixed> $data The sanitized statement model.
		 */
		// Sanitize after the filter: add-ons hook this extension point, so the markup
		// they return must not be trusted by the shortcode/block callbacks that echo it.
		return wp_kses_post( (string) apply_filters( 'tsffe_statement_html', $html, $data ) );
	}

	/**
	 * Renders the contact route on its own (for the feedback block/shortcode).
	 *
	 * @param array<string, mixed> $data
	 */
	public static function feedback_html( array $data ): string {
		$data = array_merge( TSFFE_Data::defaults(), $data );

		ob_start();
		?>
		<div class="tsffe-feedback-widget">
			<h3><?php esc_html_e( 'Accessibility feedback', 'tendforge-statement-forge-for-eaa' ); ?></h3>
			<p><?php esc_html_e( 'Found an accessibility barrier on this site? Please tell us:', 'tendforge-statement-forge-for-eaa' ); ?></p>
			<?php if ( $data['contact_email'] ) : ?>
				<p>
					<a href="<?php echo esc_attr( 'mailto:' . sanitize_email( $data['contact_email'] ) ); ?>"><?php echo esc_html( $data['contact_email'] ); ?></a>
				</p>
			<?php endif; ?>
			<?php if ( $data['feedback_method'] ) : ?>
				<p><?php echo esc_html( $data['feedback_method'] ); ?></p>
			<?php endif; ?>
		</div>
		<?php
		return wp_kses_post( trim( (string) ob_get_clean() ) );
	}

	/**
	 * @param string   $title
	 * @param string[] $items
	 * @param string   $tag   Heading tag for the bucket title (h4 standalone, h3 on the page).
	 */
	private static function bucket( string $title, array $items, string $tag = 'h4' ): void {
		if ( empty( $items ) ) {
			return;
		}
		?>
		<<?php echo esc_html( $tag ); ?>><?php echo esc_html( $title ); ?></<?php echo esc_html( $tag ); ?>>
		<ul>
			<?php foreach ( $items as $item ) : ?>
				<li><?php echo esc_html( $item ); ?></li>
			<?php endforeach; ?>
		</ul>
		<?php
	}
}
