<?php
/**
 * Export / Import page (since 2.6.0).
 *
 * Two sections: download this site's schema configuration as JSON, and restore
 * one from a file. Plus the Undo control whenever a snapshot exists.
 *
 * Expects one variable from BW_Schema_Admin::render_tools_page():
 * $notices — array of admin notices, each with a type and text.
 *
 * @package BW_AI_Schema_Pro
 */

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

$backup = BW_Schema_Config_IO::get_backup();
?>
<div class="wrap bw-schema-admin-wrap">
	<h1><?php esc_html_e( 'BW AI Schema Pro', 'bw-ai-schema-pro' ); ?></h1>

	<nav class="nav-tab-wrapper bw-schema-nav-tabs">
		<a href="<?php echo esc_url( admin_url( 'options-general.php?page=bw-ai-schema' ) ); ?>" class="nav-tab">
			<?php esc_html_e( 'Dashboard', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'options-general.php?page=bw-ai-schema-settings' ) ); ?>" class="nav-tab">
			<?php esc_html_e( 'Settings', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'options-general.php?page=bw-ai-schema-authors' ) ); ?>" class="nav-tab">
			<?php esc_html_e( 'Author Profiles', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'options-general.php?page=bw-ai-schema-setup' ) ); ?>" class="nav-tab">
			<?php esc_html_e( 'Setup Wizard', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'options-general.php?page=bw-ai-schema-tools' ) ); ?>" class="nav-tab nav-tab-active">
			<?php esc_html_e( 'Export / Import', 'bw-ai-schema-pro' ); ?>
		</a>
	</nav>

	<?php foreach ( $notices as $notice ) : ?>
		<div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible" style="margin-top:16px;">
			<p><?php echo esc_html( $notice['text'] ); ?></p>
		</div>
	<?php endforeach; ?>

	<?php if ( $backup ) : ?>
		<div class="notice notice-info" style="margin-top:16px;">
			<p style="display:flex; align-items:center; gap:14px; flex-wrap:wrap;">
				<span>
					<strong><?php esc_html_e( 'A previous configuration is saved.', 'bw-ai-schema-pro' ); ?></strong>
					<?php
					printf(
						/* translators: 1: what was done, 2: human time diff */
						esc_html__( 'Taken before "%1$s", %2$s ago.', 'bw-ai-schema-pro' ),
						esc_html( $backup['reason'] ),
						esc_html( human_time_diff( strtotime( $backup['taken_at'] ), time() ) )
					);
					?>
				</span>
				<form method="post" style="margin:0;">
					<?php wp_nonce_field( 'bw_schema_tools', 'bw_schema_tools_nonce' ); ?>
					<input type="hidden" name="bw_schema_tools_action" value="undo" />
					<button type="submit" class="button">
						<span class="dashicons dashicons-undo" style="vertical-align:text-bottom;"></span>
						<?php esc_html_e( 'Undo — restore that configuration', 'bw-ai-schema-pro' ); ?>
					</button>
				</form>
			</p>
		</div>
	<?php endif; ?>

	<!-- ===================== EXPORT ===================== -->
	<h2 style="margin-top:24px;"><?php esc_html_e( 'Export configuration', 'bw-ai-schema-pro' ); ?></h2>
	<div class="card" style="max-width:860px;">
		<p>
			<?php esc_html_e( 'Download this site\'s schema configuration as a JSON file — as a backup before a big change, to move settings to another site, or to keep alongside the project.', 'bw-ai-schema-pro' ); ?>
		</p>
		<p class="description">
			<?php esc_html_e( 'Exporting only reads your settings. It changes nothing.', 'bw-ai-schema-pro' ); ?>
		</p>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<?php wp_nonce_field( 'bw_schema_tools' ); ?>
			<input type="hidden" name="action" value="bw_schema_export_config" />

			<p>
				<label>
					<input type="checkbox" name="export_site_specific" value="1" />
					<?php esc_html_e( 'Include site-specific settings', 'bw-ai-schema-pro' ); ?>
				</label><br />
				<span class="description" style="margin-left:24px;">
					<?php esc_html_e( 'Page mappings, and the team / room post-type wiring. These reference IDs and post types that exist only on this site, so leave this off when moving settings elsewhere.', 'bw-ai-schema-pro' ); ?>
				</span>
			</p>

			<p>
				<label>
					<input type="checkbox" name="export_authors" value="1" />
					<?php esc_html_e( 'Include author profiles', 'bw-ai-schema-pro' ); ?>
				</label><br />
				<span class="description" style="margin-left:24px;">
					<?php esc_html_e( 'Custom and external author profiles, and the legacy founders list. These contain real people\'s names, emails and bios — only include them if the file is going somewhere you would be comfortable sending that data.', 'bw-ai-schema-pro' ); ?>
				</span>
			</p>

			<p>
				<button type="submit" class="button button-primary">
					<span class="dashicons dashicons-download" style="vertical-align:text-bottom;"></span>
					<?php esc_html_e( 'Download JSON', 'bw-ai-schema-pro' ); ?>
				</button>
			</p>
		</form>
	</div>

	<!-- ===================== IMPORT ===================== -->
	<h2 style="margin-top:32px;"><?php esc_html_e( 'Import configuration', 'bw-ai-schema-pro' ); ?></h2>
	<div class="card" style="max-width:860px;">
		<p>
			<?php esc_html_e( 'Restore a configuration from a JSON file exported above. Settings present in the file replace what is on this site; anything not in the file is left alone.', 'bw-ai-schema-pro' ); ?>
		</p>
		<p class="description">
			<?php esc_html_e( 'Your current configuration is saved automatically first, so an import can be undone in one click from the banner at the top of this page.', 'bw-ai-schema-pro' ); ?>
		</p>
		<form method="post" enctype="multipart/form-data">
			<?php wp_nonce_field( 'bw_schema_tools', 'bw_schema_tools_nonce' ); ?>
			<input type="hidden" name="bw_schema_tools_action" value="import" />

			<p>
				<input type="file" name="bw_schema_config_file" accept="application/json,.json" required />
			</p>

			<p>
				<label>
					<input type="checkbox" name="import_site_specific" value="1" />
					<?php esc_html_e( 'Also import site-specific settings (page mappings, post-type wiring)', 'bw-ai-schema-pro' ); ?>
				</label><br />
				<span class="description" style="margin-left:24px;">
					<?php esc_html_e( 'Only sensible when restoring onto the same site the file came from.', 'bw-ai-schema-pro' ); ?>
				</span>
			</p>

			<p>
				<label>
					<input type="checkbox" name="import_authors" value="1" />
					<?php esc_html_e( 'Also import author profiles', 'bw-ai-schema-pro' ); ?>
				</label>
			</p>

			<p>
				<button type="submit" class="button button-primary">
					<span class="dashicons dashicons-upload" style="vertical-align:text-bottom;"></span>
					<?php esc_html_e( 'Import file', 'bw-ai-schema-pro' ); ?>
				</button>
			</p>
		</form>
	</div>
</div>
