<?php
/**
 * Database Cleanup admin page shell.
 *
 * @var array<string, mixed>      $settings
 * @var array<string, mixed>|null $last_scan
 * @var array<string, mixed>      $last_clean
 * @var array<string, mixed>      $backup
 * @var array<string, mixed>      $overview
 * @var string                    $view
 * @var string                    $screen_url
 * @var string                    $history_updated
 * @var Cleanup_History_List_Table|null $history_list_table
 */

use DatabaseCleanup\Includes\Cleanup_History_List_Table;
use WP_PowerSuite\Admin\PageHero;

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

$browser_views = [ 'tables', 'options', 'post_meta', 'user_meta', 'comment_meta', 'term_meta', 'transients', 'cron', 'post_types' ];

$nav_sections = [
	[
		'label' => __( 'Cleanup', 'wp-powersuite' ),
		'items' => [
			'general'    => [ 'label' => __( 'General cleanup', 'wp-powersuite' ), 'icon' => 'dashicons-admin-generic' ],
			'tables'     => [ 'label' => __( 'Tables', 'wp-powersuite' ), 'icon' => 'dashicons-database-view' ],
			'options'    => [ 'label' => __( 'Options', 'wp-powersuite' ), 'icon' => 'dashicons-admin-settings' ],
			'post_meta'    => [ 'label' => __( 'Post meta', 'wp-powersuite' ), 'icon' => 'dashicons-admin-post' ],
			'user_meta'    => [ 'label' => __( 'User meta', 'wp-powersuite' ), 'icon' => 'dashicons-admin-users' ],
			'comment_meta' => [ 'label' => __( 'Comment meta', 'wp-powersuite' ), 'icon' => 'dashicons-admin-comments' ],
			'term_meta'    => [ 'label' => __( 'Term meta', 'wp-powersuite' ), 'icon' => 'dashicons-tag' ],
			'transients'   => [ 'label' => __( 'Transients', 'wp-powersuite' ), 'icon' => 'dashicons-clock' ],
			'cron'       => [ 'label' => __( 'Cron jobs', 'wp-powersuite' ), 'icon' => 'dashicons-calendar-alt' ],
			'post_types' => [ 'label' => __( 'Post types', 'wp-powersuite' ), 'icon' => 'dashicons-tag' ],
		],
	],
	[
		'label' => __( 'Manage', 'wp-powersuite' ),
		'items' => [
			'settings'  => [ 'label' => __( 'Settings', 'wp-powersuite' ), 'icon' => 'dashicons-admin-tools' ],
		],
	],
];

$meta_html = $view === 'general'
	? sprintf(
		'<button type="button" class="button button-primary" id="wpps-dc-scan-hero">%1$s</button>',
		esc_html__( 'Scan Database', 'wp-powersuite' )
	)
	: '';
?>
<div class="wrap wpps-dc-wrap">
	<hr class="wp-header-end" style="margin:0;padding:0;border:none;height:0;overflow:hidden;">

	<?php if ( is_multisite() ) : ?>
		<div class="notice notice-info">
			<p>
				<?php
				printf(
					/* translators: %s: current site name */
					esc_html__( 'Multisite: cleanup runs on the current site only (%s). User meta changes apply network-wide. Switch to a site before cleaning its tables.', 'wp-powersuite' ),
					esc_html( get_bloginfo( 'name' ) )
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $backup['show_warning'] ) ) : ?>
		<div class="notice notice-warning is-dismissible">
			<p>
				<?php
				if ( ! empty( $backup['module_enabled'] ) && ! empty( $backup['url'] ) ) {
					printf(
						wp_kses_post( __( 'No recent backup found in the last %1$d days. <a href="%2$s">Create a backup</a> before running database cleanup.', 'wp-powersuite' ) ),
						(int) ( $backup['warn_days'] ?? 7 ),
						esc_url( (string) $backup['url'] )
					);
				} else {
					esc_html_e( 'Consider creating a full site backup before running database cleanup.', 'wp-powersuite' );
				}
				?>
			</p>
		</div>
	<?php elseif ( ! empty( $backup['module_enabled'] ) && ! empty( $backup['url'] ) ) : ?>
		<div class="notice notice-info inline wpps-dc-privacy-notice">
			<p>
				<?php
				printf(
					wp_kses_post( __( 'Backup reminder: <a href="%s">Open Backup & Restore</a> before deleting data.', 'wp-powersuite' ) ),
					esc_url( (string) $backup['url'] )
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<?php
	PageHero::render_for_module(
		[
			'icon'      => 'dashicons-database',
			'title'     => __( 'Database Cleanup', 'wp-powersuite' ),
			'tagline'   => __( 'Scan, browse, and clean database bloat — like a dedicated DB cleaner, built into PowerSuite.', 'wp-powersuite' ),
			'meta_html' => $meta_html,
		]
	);
	?>

	<div id="wpps-dc-notices"></div>

	<?php if ( $view === 'settings' && $history_updated !== '' ) : ?>
		<?php
		if ( $history_updated === 'cleared' ) {
			$history_notice = __( 'Cleanup history cleared.', 'wp-powersuite' );
		} elseif ( str_starts_with( $history_updated, 'deleted_' ) ) {
			$deleted_count  = max( 0, (int) substr( $history_updated, 8 ) );
			$history_notice = sprintf(
				_n( '%d history entry deleted.', '%d history entries deleted.', $deleted_count, 'wp-powersuite' ),
				$deleted_count
			);
		} else {
			$history_notice = '';
		}
		?>
		<?php if ( $history_notice !== '' ) : ?>
			<div class="notice notice-success is-dismissible"><p><?php echo esc_html( $history_notice ); ?></p></div>
		<?php endif; ?>
	<?php endif; ?>

	<nav class="wpps-dc-section-nav" aria-label="<?php esc_attr_e( 'Database cleanup sections', 'wp-powersuite' ); ?>">
		<?php foreach ( $nav_sections as $section_index => $section ) : ?>
			<span class="wpps-dc-section-nav__group<?php echo $section_index > 0 ? ' wpps-dc-section-nav__group--divided' : ''; ?>" role="group" aria-label="<?php echo esc_attr( (string) ( $section['label'] ?? '' ) ); ?>">
				<?php foreach ( (array) ( $section['items'] ?? [] ) as $item_id => $item ) : ?>
					<?php
					$item_url = add_query_arg( 'view', $item_id, $screen_url );
					$active   = $view === $item_id;
					?>
					<a href="<?php echo esc_url( $item_url ); ?>" class="wpps-dc-section-nav__item<?php echo $active ? ' is-active' : ''; ?>"<?php echo $active ? ' aria-current="page"' : ''; ?>>
						<span class="dashicons <?php echo esc_attr( (string) ( $item['icon'] ?? 'dashicons-admin-generic' ) ); ?>" aria-hidden="true"></span>
						<span class="wpps-dc-section-nav__label"><?php echo esc_html( (string) ( $item['label'] ?? '' ) ); ?></span>
					</a>
				<?php endforeach; ?>
			</span>
		<?php endforeach; ?>
	</nav>

	<div class="wpps-dc-content">
		<?php
		if ( $view === 'general' ) {
			include __DIR__ . '/views/general-cleanup.php';
		} elseif ( in_array( $view, $browser_views, true ) ) {
			$browser_type = $view;
			include __DIR__ . '/views/browser.php';
		} elseif ( $view === 'settings' ) {
			include __DIR__ . '/views/settings.php';
		}
		?>
	</div>
</div>

<div id="wpps-dc-item-modal" class="wpps-dc-modal-overlay" role="dialog" aria-modal="true" aria-labelledby="wpps-dc-item-modal-title" aria-hidden="true">
	<div class="wpps-dc-modal-container wpps-dc-modal-container--wide">
		<div class="wpps-dc-modal-header">
			<h2 id="wpps-dc-item-modal-title"><?php esc_html_e( 'Item details', 'wp-powersuite' ); ?></h2>
			<button type="button" class="wpps-dc-modal-close" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
		</div>
		<div class="wpps-dc-modal-content" id="wpps-dc-item-modal-content">
			<div class="wpps-dc-loading"><?php esc_html_e( 'Loading…', 'wp-powersuite' ); ?></div>
		</div>
	</div>
</div>

<div id="wpps-dc-history-modal" class="wpps-dc-modal-overlay" role="dialog" aria-modal="true" aria-labelledby="wpps-dc-history-modal-title" aria-hidden="true">
	<div class="wpps-dc-modal-container">
		<div class="wpps-dc-modal-header">
			<h2 id="wpps-dc-history-modal-title"><?php esc_html_e( 'Cleanup run details', 'wp-powersuite' ); ?></h2>
			<button type="button" class="wpps-dc-modal-close" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
		</div>
		<div class="wpps-dc-modal-content" id="wpps-dc-history-modal-content">
			<div class="wpps-dc-loading"><?php esc_html_e( 'Loading…', 'wp-powersuite' ); ?></div>
		</div>
	</div>
</div>

<div id="wpps-dc-confirm-modal" class="wpps-dc-modal-overlay wpps-dc-confirm-modal" role="dialog" aria-modal="true" aria-labelledby="wpps-dc-confirm-title" aria-describedby="wpps-dc-confirm-text" aria-hidden="true">
	<div class="wpps-dc-modal-container wpps-dc-modal-container--confirm">
		<div class="wpps-dc-modal-header">
			<h2 id="wpps-dc-confirm-title"><?php esc_html_e( 'Confirm cleanup', 'wp-powersuite' ); ?></h2>
			<button type="button" class="wpps-dc-modal-close" data-confirm-cancel aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
		</div>
		<div class="wpps-dc-modal-content">
			<p id="wpps-dc-confirm-text" class="wpps-dc-confirm-text"></p>
			<div class="wpps-dc-confirm-summary" id="wpps-dc-confirm-summary"></div>
			<div class="wpps-dc-confirm-backup" id="wpps-dc-confirm-backup" hidden>
				<span class="dashicons dashicons-shield" aria-hidden="true"></span>
				<span class="wpps-dc-confirm-backup__text"></span>
			</div>
			<div class="wpps-dc-confirm-progress" id="wpps-dc-confirm-progress" hidden>
				<div class="wpps-dc-progress-bar"><span id="wpps-dc-confirm-progress-fill"></span></div>
				<p id="wpps-dc-confirm-progress-text"></p>
			</div>
			<div class="wpps-dc-confirm-result" id="wpps-dc-confirm-result" role="status" hidden></div>
			<div class="wpps-dc-confirm-breakdown" id="wpps-dc-confirm-breakdown" hidden></div>
			<div class="wpps-dc-confirm-error" id="wpps-dc-confirm-error" role="alert" hidden></div>
		</div>
		<div class="wpps-dc-modal-footer">
			<button type="button" class="button" id="wpps-dc-confirm-cancel" data-confirm-cancel><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
			<button type="button" class="button button-primary" id="wpps-dc-confirm-accept">
				<span class="wpps-dc-spinner" aria-hidden="true" hidden></span>
				<span class="wpps-dc-confirm-accept__label"><?php esc_html_e( 'Confirm cleanup', 'wp-powersuite' ); ?></span>
			</button>
		</div>
	</div>
</div>
