<?php
/**
 * Full-page restore UI (opens in a new tab from the backup list).
 *
 * @package BackupRestore
 *
 * @var array{id:string,label:string,components:list<string>}|null $br_restore_ctx
 * @var string $br_restore_error
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WP_PowerSuite\Admin\PageHero;

$parts_labels = [
	'database' => __( 'Database', 'wp-powersuite' ),
	'plugins'  => __( 'Plugins', 'wp-powersuite' ),
	'themes'   => __( 'Themes', 'wp-powersuite' ),
	'uploads'  => __( 'Uploads', 'wp-powersuite' ),
	'others'   => __( 'Other wp-content files', 'wp-powersuite' ),
];
$order        = [ 'database', 'plugins', 'themes', 'uploads', 'others' ];
$main_url     = admin_url( 'admin.php?page=powersuite-backup-restore' );
$ctx          = is_array( $br_restore_ctx ?? null ) ? $br_restore_ctx : null;
$err          = isset( $br_restore_error ) ? (string) $br_restore_error : '';
?>
<div class="wrap wpps-br-wrap wpps-br-restore-screen">
	<hr class="wp-header-end" />
	<?php if ( $err !== '' ) : ?>
		<div class="notice notice-error"><p><?php echo esc_html( $err ); ?></p></div>
	<?php elseif ( $ctx && isset( $ctx['id'], $ctx['components'] ) && is_array( $ctx['components'] ) ) : ?>
		<?php
		$bid   = (string) $ctx['id'];
		$blbl  = (string) ( $ctx['label'] ?? $bid );
		$comps = $ctx['components'];
		PageHero::render_for_module(
			[
				'icon'    => 'dashicons-backup',
				'title'   => sprintf(
					/* translators: %s: backup date label or id */
					__( 'Restore from backup — %s', 'wp-powersuite' ),
					$blbl
				),
				'tagline' => __( 'Choose components, verify files on this server, then run the restore.', 'wp-powersuite' ),
				'actions' => [
					[
						'label' => __( 'Backup & Restore', 'wp-powersuite' ),
						'url'   => $main_url,
						'icon'  => 'dashicons-arrow-left-alt',
					],
				],
			]
		);
		?>
		<div
			id="wpps-br-restore-flow"
			class="wpps-br-restore-flow-sheet"
			data-restore-backup-id="<?php echo esc_attr( $bid ); ?>"
			data-restore-backup-label="<?php echo esc_attr( $blbl ); ?>"
		>
			<div class="wpps-br-restore-flow-body">
				<ol class="wpps-br-restore-rail" role="tablist" aria-label="<?php esc_attr_e( 'Restore steps', 'wp-powersuite' ); ?>">
					<li class="wpps-br-restore-rail__step wpps-br-restore-rail__step--active" data-step-dot="1">
						<span class="wpps-br-restore-rail__num">1</span>
						<span class="wpps-br-restore-rail__label"><?php esc_html_e( 'Prepare', 'wp-powersuite' ); ?></span>
					</li>
					<li class="wpps-br-restore-rail__step" data-step-dot="2">
						<span class="wpps-br-restore-rail__num">2</span>
						<span class="wpps-br-restore-rail__label"><?php esc_html_e( 'Verify', 'wp-powersuite' ); ?></span>
					</li>
					<li class="wpps-br-restore-rail__step" data-step-dot="3">
						<span class="wpps-br-restore-rail__num">3</span>
						<span class="wpps-br-restore-rail__label"><?php esc_html_e( 'Run restore', 'wp-powersuite' ); ?></span>
					</li>
				</ol>

				<div id="wpps-br-restore-step-prepare" class="wpps-br-restore-pane">
					<div class="notice notice-warning inline wpps-br-restore-privacy-notice">
						<p><?php esc_html_e( 'Restoring replaces live database and/or files on this server. Keep a separate backup elsewhere before continuing. When the database is restored, maintenance mode is enabled for visitors during that step.', 'wp-powersuite' ); ?></p>
					</div>
					<div id="wpps-br-restore-pick-block">
						<p class="wpps-br-restore-intro"><?php esc_html_e( 'Choose what to restore, then verify files on this server before running the restore. This replaces matching data on the current site.', 'wp-powersuite' ); ?></p>
						<fieldset class="wpps-br-restore-fieldset">
							<legend class="wpps-br-restore-legend"><?php esc_html_e( 'Components', 'wp-powersuite' ); ?></legend>
							<div id="wpps-br-restore-checkboxes" class="wpps-br-restore-checkboxes">
								<?php
								foreach ( $order as $key ) {
									if ( ! in_array( $key, $comps, true ) ) {
										continue;
									}
									$id    = 'wpps-br-rc-' . $key;
									$label = $parts_labels[ $key ] ?? $key;
									echo '<p class="wpps-br-restore-row"><label for="' . esc_attr( $id ) . '">';
									echo '<input type="checkbox" id="' . esc_attr( $id ) . '" name="wpps_br_restore_part[]" value="' . esc_attr( $key ) . '" checked /> ';
									echo esc_html( $label );
									echo '</label></p>';
								}
								?>
							</div>
						</fieldset>
					</div>

					<p class="wpps-br-dialog__actions wpps-br-dialog__actions--spread wpps-br-restore-footer-actions">
						<button type="button" class="button wpps-br-restore-dismiss"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
						<span class="wpps-br-restore-prepare-actions">
							<button type="button" class="button button-primary" id="wpps-br-restore-continue"><?php esc_html_e( 'Verify selected files', 'wp-powersuite' ); ?></button>
						</span>
					</p>
				</div>

				<div id="wpps-br-restore-step-verify" class="wpps-br-restore-pane" hidden>
					<div id="wpps-br-restore-verify-block" class="wpps-br-restore-verify-block">
						<h2 class="wpps-br-restore-verify-heading"><?php esc_html_e( 'Verify restore readiness', 'wp-powersuite' ); ?></h2>
						<p class="wpps-br-restore-step-desc"><?php esc_html_e( 'We confirm each selected backup part on this server first. Additional PHP and server checks for your selection appear below (expand to review).', 'wp-powersuite' ); ?></p>
						<div id="wpps-br-restore-prepare-loading" class="wpps-br-restore-prepare-loading" hidden>
							<span class="spinner is-active" aria-hidden="true"></span>
							<span><?php esc_html_e( 'Checking files…', 'wp-powersuite' ); ?></span>
						</div>
						<div id="wpps-br-restore-prepare-success" class="wpps-br-restore-prepare-success" hidden></div>
						<div class="wpps-br-restore-parts-wrap">
							<h3 class="wpps-br-restore-verify-subhead wpps-br-restore-verify-subhead--parts"><?php esc_html_e( 'Selected parts', 'wp-powersuite' ); ?></h3>
							<div id="wpps-br-restore-prepare-list" class="wpps-br-restore-prepare-list-host"></div>
						</div>
						<details id="wpps-br-restore-server-section" class="wpps-br-restore-server-acc" hidden>
							<summary class="wpps-br-restore-server-acc__summary">
								<?php esc_html_e( 'Server & PHP checks', 'wp-powersuite' ); ?>
							</summary>
							<div id="wpps-br-restore-server-checks" class="wpps-br-restore-server-checks"></div>
						</details>
						<div id="wpps-br-restore-prepare-block" class="wpps-br-restore-prepare-block" hidden></div>
						<div id="wpps-br-restore-prepare-warn" class="wpps-br-restore-prepare-warn" hidden></div>
					</div>

					<p class="wpps-br-dialog__actions wpps-br-dialog__actions--spread wpps-br-restore-footer-actions">
						<button type="button" class="button wpps-br-restore-dismiss"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
						<span class="wpps-br-restore-prepare-actions">
							<button type="button" class="button" id="wpps-br-restore-change-selection" hidden><?php esc_html_e( 'Change selection', 'wp-powersuite' ); ?></button>
							<button type="button" class="button button-primary" id="wpps-br-restore-start" hidden disabled><?php esc_html_e( 'Start restore', 'wp-powersuite' ); ?></button>
						</span>
					</p>
				</div>

				<div id="wpps-br-restore-step-run" class="wpps-br-restore-pane wpps-br-restore-pane--progress" hidden>
					<div id="wpps-br-restore-run-during">
						<p id="wpps-br-restore-modal-lead" class="wpps-br-restore-modal-lead"></p>
						<div class="wpps-br-restore-run-panel">
							<ul id="wpps-br-restore-modal-phases" class="wpps-br-restore-modal-phases" aria-live="polite"></ul>
							<div class="wpps-br-progress-bar-row wpps-br-restore-modal-track-wrap">
								<div
									class="wpps-br-progress-track wpps-br-restore-modal-track"
									role="progressbar"
									aria-valuemin="0"
									aria-valuemax="100"
									aria-valuenow="0"
									id="wpps-br-restore-modal-track"
								>
									<div class="wpps-br-progress-fill wpps-br-restore-modal-fill" id="wpps-br-restore-modal-fill" style="width:0%;"></div>
								</div>
								<span class="wpps-br-progress-pct" id="wpps-br-restore-modal-pct" aria-hidden="true">0%</span>
							</div>
							<p class="wpps-br-restore-status-line" id="wpps-br-restore-modal-status"></p>
							<div class="wpps-br-restore-modal-meta">
								<span class="wpps-br-restore-modal-meta__activity" id="wpps-br-restore-modal-activity"></span>
								<span class="wpps-br-restore-modal-meta__actions">
									<button
										type="button"
										class="button wpps-br-progress-meta__btn"
										id="wpps-br-show-restore-live-log"
										aria-expanded="false"
										aria-controls="wpps-br-restore-live-log-section"
									><?php esc_html_e( 'Show log', 'wp-powersuite' ); ?></button>
									<button type="button" class="button wpps-br-progress-meta__btn" id="wpps-br-restore-modal-stop"><?php esc_html_e( 'Stop restore', 'wp-powersuite' ); ?></button>
								</span>
							</div>
							<div id="wpps-br-restore-live-log-section" class="wpps-br-backup-live-log-wrap" hidden>
								<h4 class="wpps-br-backup-live-log-title"><?php esc_html_e( 'Live log', 'wp-powersuite' ); ?></h4>
								<pre id="wpps-br-restore-live-log-pre" class="wpps-br-live-log wpps-br-backup-live-log-pre" aria-live="polite"></pre>
							</div>
						</div>
						<p class="wpps-br-dialog__actions wpps-br-dialog__actions--spread wpps-br-restore-run-actions">
							<span></span>
							<button type="button" class="button wpps-br-restore-dismiss" id="wpps-br-restore-cancel-during"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
						</p>
					</div>
					<div id="wpps-br-restore-complete-panel" class="wpps-br-restore-complete-panel" hidden>
						<div class="wpps-br-restore-celebrate" role="status" aria-live="polite">
							<div class="wpps-br-restore-celebrate__burst" aria-hidden="true">
								<span class="wpps-br-restore-celebrate__emoji">🎉</span>
							</div>
							<h2 class="wpps-br-restore-celebrate__title"><?php esc_html_e( 'Congratulations!', 'wp-powersuite' ); ?></h2>
							<p class="wpps-br-restore-celebrate__lead"><?php esc_html_e( 'Your backup was restored successfully.', 'wp-powersuite' ); ?></p>
							<p class="wpps-br-restore-celebrate__hint" id="wpps-br-restore-complete-hint"></p>
						</div>
						<div id="wpps-br-restore-elementor-notice" class="wpps-br-restore-elementor-notice" hidden>
							<span class="dashicons dashicons-info-outline"></span>
							<div>
								<strong><?php esc_html_e( 'Elementor detected', 'wp-powersuite' ); ?></strong>
								<p><?php esc_html_e( 'Some Elementor URLs may still reference the old domain. Go to Elementor > Tools > Replace URL to update them, then clear your caches.', 'wp-powersuite' ); ?></p>
							</div>
						</div>
						<p class="wpps-br-restore-complete-actions">
							<a href="/" class="button button-primary" id="wpps-br-restore-visit-site" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Visit site', 'wp-powersuite' ); ?></a>
							<a href="#" class="button" id="wpps-br-restore-dl-log"><?php esc_html_e( 'Download restore log', 'wp-powersuite' ); ?></a>
							<button type="button" class="button" id="wpps-br-restore-copy-log" hidden><?php esc_html_e( 'Copy log', 'wp-powersuite' ); ?></button>
							<button type="button" class="button" id="wpps-br-restore-reload-admin"><?php esc_html_e( 'Reload this page', 'wp-powersuite' ); ?></button>
							<button type="button" class="button wpps-br-restore-dismiss"><?php esc_html_e( 'Close', 'wp-powersuite' ); ?></button>
						</p>
					</div>
				</div>
			</div>
		</div>

		<div id="wpps-br-restore-confirm" class="wpps-br-overlay" hidden>
			<div class="wpps-br-overlay__backdrop" id="wpps-br-restore-confirm-backdrop" aria-hidden="true"></div>
			<div class="wpps-br-overlay__dialog" role="dialog" aria-modal="true" aria-labelledby="wpps-br-restore-confirm-title">
				<h2 id="wpps-br-restore-confirm-title" class="wpps-br-overlay__title"><?php esc_html_e( 'Start restore?', 'wp-powersuite' ); ?></h2>
				<div id="wpps-br-restore-confirm-body" class="wpps-br-overlay__body"></div>
				<p class="wpps-br-overlay__actions">
					<button type="button" class="button" id="wpps-br-restore-confirm-cancel"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-primary" id="wpps-br-restore-confirm-proceed"><?php esc_html_e( 'Start restore', 'wp-powersuite' ); ?></button>
				</p>
			</div>
		</div>
	<?php endif; ?>
</div>
