<?php
/**
 * Sync Jobs admin-page renderer.
 *
 * Phase 11 slice 11f server-side renderer for the §6 Sync Jobs admin
 * page documented in `specifications/admin-ui-spec.md`. The renderer
 * drives {@see ListSyncJobsUseCase} (paginated row slice — items already
 * carry wire-vocabulary status per §6.0) and echoes the page HTML —
 * filter form, paginated results table, empty-state message, and
 * pagination nav — into the admin output stream.
 *
 * Capability gating is NOT performed by this renderer — the
 * page-render closure in
 * {@see \Affilun\Presentation\Admin\AdminServiceProvider}
 * re-checks the §3 admin-tier capability and `wp_die`'s on denial.
 * The renderer just emits HTML.
 *
 * Wire ⇔ DB status: rows reaching this renderer have already been
 * projected through {@see ListSyncJobsUseCase}'s DB → wire `pending`
 * → `queued` substitution, so the table's `status` column carries
 * wire vocabulary verbatim. The filter form's status `<select>` also
 * speaks wire vocabulary; {@see SyncJobsListQueryParser} performs
 * the inverse `queued` → `pending` translation when constructing the
 * query VO.
 *
 * Escaping invariants:
 *
 *   - Every visible text emission flows through `esc_html()`.
 *   - Every attribute value flows through `esc_attr()`.
 *   - Every URL flows through `esc_url()`.
 *
 * @package Affilun\Presentation\Admin\Page
 */

declare(strict_types=1);

// phpcs:disable Generic.Files.LineLength.TooLong -- Table-cell render expressions are clearer kept as one escaped echo per column.

namespace Affilun\Presentation\Admin\Page;

use Affilun\Application\SyncJobs\UseCase\ListSyncJobsUseCase;
use Affilun\Infrastructure\Persistence\SyncJobListQuery;
use Affilun\Presentation\Admin\AdminTimeFormatter;

/**
 * Renders the §6 Sync Jobs admin page to stdout.
 *
 * See the file-level docblock for the page layout (filter form,
 * results table, pagination), the wire ⇔ DB status posture, and the
 * escaping invariants.
 */
final class SyncJobsPageRenderer {

	/**
	 * Admin menu slug the filter form / pagination links point at.
	 */
	private const MENU_SLUG = 'affilun-sync-jobs';

	/**
	 * Page-size choices exposed on admin tables.
	 *
	 * @var list<int>
	 */
	private const PER_PAGE_OPTIONS = array( 10, 20, 50 );

	/**
	 * Wire-vocabulary statuses surfaced in the filter `<select>` per
	 * `specifications/rest-api-spec.md` §6.0. The DB-vocabulary
	 * `pending` is rewritten to wire `queued` here so the dropdown
	 * matches the table's status column.
	 *
	 * @var list<string>
	 */
	private const WIRE_STATUSES = array(
		'queued',
		'running',
		'completed',
		'failed',
		'cancelled',
	);

	/**
	 * §11.3 list use case.
	 */
	private ListSyncJobsUseCase $listUseCase;

	/**
	 * Construct the renderer with explicit collaborator injection.
	 *
	 * @param ListSyncJobsUseCase $listUseCase §11.3 list use case.
	 */
	public function __construct( ListSyncJobsUseCase $listUseCase ) {
		$this->listUseCase = $listUseCase;
	}

	/**
	 * Echo the §6 Sync Jobs page HTML to stdout.
	 *
	 * @param SyncJobListQuery $query Already-validated list query.
	 */
	public function render( SyncJobListQuery $query ): void {
		echo '<div class="wrap">';
		echo '<h1>' . esc_html( '同期ジョブ' ) . '</h1>';
		$this->renderEmbedded( $query, self::MENU_SLUG, '' );
		echo '</div>';
	}

	/**
	 * Echo sync-job content without the outer admin-page wrapper.
	 *
	 * @param SyncJobListQuery $query    Current query.
	 * @param string           $menuSlug Admin page slug for embedded forms.
	 * @param string           $anchor   Optional URL fragment to preserve tab focus.
	 */
	public function renderEmbedded( SyncJobListQuery $query, string $menuSlug = self::MENU_SLUG, string $anchor = '' ): void {
		$this->renderFilterForm( $query, $menuSlug, $anchor );

		$result = $this->listUseCase->execute( $query );

		if ( $result->items() === array() ) {
			echo '<p>' . esc_html( '条件に一致する同期ジョブはありません。' ) . '</p>';
			return;
		}

		echo '<table class="wp-list-table widefat fixed striped">';
		$this->renderTableHead();
		echo '<tbody>';
		foreach ( $result->items() as $row ) {
			$this->renderRow( $row );
		}
		echo '</tbody>';
		echo '</table>';

		$this->renderPagination( $query, $result->pageCount(), $result->total(), $menuSlug, $anchor );
	}

	/**
	 * Render the §6 filter form (status / job_type selects).
	 *
	 * The form submits via GET back to the same admin page so the
	 * parsed query state survives a page reload; the implicit
	 * `page=affilun-sync-jobs` hidden field keeps the WordPress admin
	 * router pointing at this page across the round-trip.
	 *
	 * @param SyncJobListQuery $query    Current query (drives selected option state).
	 * @param string           $menuSlug Admin page slug for the form round-trip.
	 * @param string           $anchor   Optional URL fragment to preserve tab focus.
	 */
	private function renderFilterForm( SyncJobListQuery $query, string $menuSlug = self::MENU_SLUG, string $anchor = '' ): void {
		$selectedStatus = $query->status();
		// Round-trip the DB `pending` back to wire `queued` for the
		// dropdown so the visible selected option matches the
		// dropdown's wire-vocabulary labels.
		if ( $selectedStatus === 'pending' ) {
			$selectedStatus = 'queued';
		}

		echo '<form method="get" action="' . esc_url( self::formActionUrl( $anchor ) ) . '">';
		echo '<input type="hidden" name="page" value="' . esc_attr( $menuSlug ) . '">';
		if ( $query->perPage() !== SyncJobListQuery::DEFAULT_PER_PAGE ) {
			echo '<input type="hidden" name="' . esc_attr( SyncJobsListQueryParser::KEY_PER_PAGE ) . '" value="' . esc_attr( (string) $query->perPage() ) . '">';
		}

		echo '<p class="search-box">';

		// Status.
		echo '<label for="affilun-filter-status">' . esc_html( 'ステータス' ) . '</label> ';
		echo '<select name="' . esc_attr( SyncJobsListQueryParser::KEY_STATUS ) . '" id="affilun-filter-status">';
		echo '<option value="">' . esc_html( 'すべて' ) . '</option>';
		foreach ( self::WIRE_STATUSES as $status ) {
			$selected = $selectedStatus === $status ? ' selected="selected"' : '';
			echo '<option value="' . esc_attr( $status ) . '"'
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $selected is one of two hard-coded literals.
				. $selected
				. '>' . esc_html( $status ) . '</option>';
		}
		echo '</select> ';

		// Job type.
		echo '<label for="affilun-filter-job-type">' . esc_html( 'ジョブ種別' ) . '</label> ';
		echo '<select name="' . esc_attr( SyncJobsListQueryParser::KEY_JOB_TYPE ) . '" id="affilun-filter-job-type">';
		echo '<option value="">' . esc_html( 'すべて' ) . '</option>';
		foreach ( SyncJobListQuery::validJobTypes() as $jobType ) {
			$selected = $query->jobType() === $jobType ? ' selected="selected"' : '';
			echo '<option value="' . esc_attr( $jobType ) . '"'
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $selected is one of two hard-coded literals.
				. $selected
				. '>' . esc_html( $jobType ) . '</option>';
		}
		echo '</select> ';

		echo '<button type="submit" class="button">' . esc_html( '絞り込み' ) . '</button>';
		echo '</p>';

		echo '</form>';
	}

	/**
	 * Render the §6 results table `<thead>`.
	 */
	private function renderTableHead(): void {
		echo '<thead><tr>';
		foreach (
			array(
				'ID',
				'ジョブ種別',
				'ステータス',
				'優先度',
				'試行回数',
				'作成日時',
				'実行可能時刻',
				'完了時刻',
				'最終エラー',
			) as $heading
		) {
			echo '<th scope="col">' . esc_html( $heading ) . '</th>';
		}
		echo '</tr></thead>';
	}

	/**
	 * Render a single row.
	 *
	 * @param array<string, mixed> $row Wire-shape row from the use case.
	 */
	private function renderRow( array $row ): void {
		$id          = $row['id'] ?? null;
		$jobType     = $row['job_type'] ?? '';
		$status      = $row['status'] ?? '';
		$priority    = $row['priority'] ?? null;
		$attempts    = $row['attempts'] ?? 0;
		$createdAt   = $row['created_at'] ?? null;
		$availableAt = $row['available_at'] ?? null;
		$finishedAt  = $row['finished_at'] ?? null;
		$lastError   = $row['last_error'] ?? null;

		echo '<tr>';
		echo '<td>' . esc_html( $id === null ? '' : (string) $id ) . '</td>';
		echo '<td>' . esc_html( (string) $jobType ) . '</td>';
		echo '<td>' . esc_html( (string) $status ) . '</td>';
		echo '<td>' . esc_html( $priority === null ? '' : (string) (int) $priority ) . '</td>';
		echo '<td>' . esc_html( (string) (int) $attempts ) . '</td>';
		echo '<td>' . esc_html( AdminTimeFormatter::format( $createdAt === null ? null : (string) $createdAt ) ) . '</td>';
		echo '<td>' . esc_html( AdminTimeFormatter::format( $availableAt === null ? null : (string) $availableAt ) ) . '</td>';
		echo '<td>' . esc_html( AdminTimeFormatter::format( $finishedAt === null ? null : (string) $finishedAt ) ) . '</td>';
		echo '<td>' . esc_html( $lastError === null ? '' : (string) $lastError ) . '</td>';
		echo '</tr>';
	}

	/**
	 * Render the textual `Page X of Y` pagination nav with prev /
	 * next anchors that preserve the current filter state.
	 *
	 * @param SyncJobListQuery $query      Originating query.
	 * @param int              $totalPages Total page count.
	 * @param int              $totalItems Total item count.
	 * @param string           $menuSlug   Admin page slug for generated links.
	 * @param string           $anchor     Optional URL fragment to preserve tab focus.
	 */
	private function renderPagination( SyncJobListQuery $query, int $totalPages, int $totalItems, string $menuSlug = self::MENU_SLUG, string $anchor = '' ): void {
		$current = $query->page();

		echo '<div class="tablenav"><div class="tablenav-pages">';
		echo '<span class="displaying-num">';
		echo esc_html( sprintf( '%d件', $totalItems ) );
		echo '</span>';
		$this->renderPerPageLinks( $query, $menuSlug, $anchor );

		if ( $totalPages > 1 ) {
			echo ' <span class="pagination-links">';

			if ( $current > 1 ) {
				$prevUrl = self::buildPageUrl( $current - 1, $query, $menuSlug, $anchor );
				echo '<a class="prev-page button" href="' . esc_url( $prevUrl ) . '">'
					. esc_html( '« 前へ' ) . '</a> ';
			}

			echo '<span class="paging-input">' . esc_html( sprintf( '%d / %d', $current, max( 1, $totalPages ) ) ) . '</span> ';

			if ( $current < $totalPages ) {
				$nextUrl = self::buildPageUrl( $current + 1, $query, $menuSlug, $anchor );
				echo '<a class="next-page button" href="' . esc_url( $nextUrl ) . '">'
					. esc_html( '次へ »' ) . '</a>';
			}

			echo '</span>';
		} else {
			echo ' <span class="paging-input">' . esc_html( sprintf( '%d / %d', $current, max( 1, $totalPages ) ) ) . '</span>';
		}

		echo '</div></div>';
	}

	/**
	 * Render 10/20/50 page-size links.
	 *
	 * @param SyncJobListQuery $query    Current query.
	 * @param string           $menuSlug Admin page slug for generated links.
	 * @param string           $anchor   Optional URL fragment to preserve tab focus.
	 */
	private function renderPerPageLinks( SyncJobListQuery $query, string $menuSlug, string $anchor ): void {
		echo ' <span class="affilun-per-page-links">';
		echo esc_html( '表示件数: ' );
		$first = true;
		foreach ( self::PER_PAGE_OPTIONS as $option ) {
			if ( ! $first ) {
				echo ' / ';
			}
			if ( $option === $query->perPage() ) {
				echo '<strong>' . esc_html( sprintf( '%d件', $option ) ) . '</strong>';
			} else {
				echo '<a href="' . esc_url( self::buildPageUrl( 1, $query->withPerPage( $option ), $menuSlug, $anchor ) ) . '">' . esc_html( sprintf( '%d件', $option ) ) . '</a>';
			}
			$first = false;
		}
		echo '</span>';
	}

	/**
	 * Build the per-page URL preserving the current filter state.
	 *
	 * The `status` axis is rewritten back to wire vocabulary
	 * (`pending` → `queued`) before being emitted into the URL so
	 * the round-trip reaches {@see SyncJobsListQueryParser} in the
	 * same wire shape the original filter form submitted.
	 *
	 * Prefers `add_query_arg()` (the WordPress helper handles
	 * existing query-string fragments correctly) and falls back to
	 * manual `http_build_query()` concatenation in pure-unit
	 * harnesses where WordPress is not loaded.
	 *
	 * @param int              $page     Target page (>= 1).
	 * @param SyncJobListQuery $query    Current query state.
	 * @param string           $menuSlug Admin page slug for generated links.
	 * @param string           $anchor   Optional URL fragment to preserve tab focus.
	 */
	private static function buildPageUrl( int $page, SyncJobListQuery $query, string $menuSlug = self::MENU_SLUG, string $anchor = '' ): string {
		$args = array(
			'page'                            => $menuSlug,
			SyncJobsListQueryParser::KEY_PAGE => $page,
		);

		$status = $query->status();
		if ( $status !== null ) {
			if ( $status === 'pending' ) {
				$status = 'queued';
			}
			$args[ SyncJobsListQueryParser::KEY_STATUS ] = $status;
		}
		if ( $query->jobType() !== null ) {
			$args[ SyncJobsListQueryParser::KEY_JOB_TYPE ] = $query->jobType();
		}
		if ( $query->perPage() !== SyncJobListQuery::DEFAULT_PER_PAGE ) {
			$args[ SyncJobsListQueryParser::KEY_PER_PAGE ] = $query->perPage();
		}

		$base = self::resolveAdminUrl( 'admin.php' );

		if ( function_exists( 'add_query_arg' ) ) {
			return (string) add_query_arg( $args, $base ) . $anchor;
		}

		$separator = str_contains( $base, '?' ) ? '&' : '?';
		return $base . $separator . http_build_query( $args ) . $anchor;
	}

	/**
	 * Resolve a form action URL for embedded filter forms.
	 *
	 * @param string $anchor Optional URL fragment to preserve tab focus.
	 */
	private static function formActionUrl( string $anchor ): string {
		return $anchor === '' ? '' : self::resolveAdminUrl( 'admin.php' ) . $anchor;
	}

	/**
	 * Resolve an admin URL via `admin_url()` when available,
	 * falling back to a synthetic absolute URL in pure-unit
	 * harnesses where WordPress is not loaded so `esc_url()` does
	 * not strip the value.
	 *
	 * @param string $path Admin path (e.g. `'admin.php'`).
	 */
	private static function resolveAdminUrl( string $path ): string {
		if ( function_exists( 'admin_url' ) ) {
			return (string) admin_url( $path );
		}

		return 'http://example.test/wp-admin/' . ltrim( $path, '/' );
	}
}
