<?php
/**
 * Dashboard page view.
 *
 * @package PrivexPasswordProtect
 */

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

$ppmstr_settings = PPMSTR_Settings::get_all();
$ppmstr_site_on  = ! empty( $ppmstr_settings['site_protection_enabled'] );

// Count protected content.
$ppmstr_protected_posts = new WP_Query(
	array(
		'post_type'      => array( 'post', 'page' ),
		'meta_key'       => '_ppmstr_protected', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		'meta_value'     => '1', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
		'posts_per_page' => -1,
		'fields'         => 'ids',
		'no_found_rows'  => true,
	)
);
$ppmstr_protected_count = $ppmstr_protected_posts->post_count;
wp_reset_postdata();

$ppmstr_cat_count = count( isset( $ppmstr_settings['protected_categories'] ) ? $ppmstr_settings['protected_categories'] : array() );
?>

<div class="ppmstr-page-header">
	<h2><?php esc_html_e( 'Dashboard', 'privex-password-protect' ); ?></h2>
	<p class="ppmstr-page-desc"><?php esc_html_e( 'Welcome to Privex Password Protect. Here\'s an overview of your protection status.', 'privex-password-protect' ); ?></p>
</div>

<!-- Stats Row -->
<div class="ppmstr-stats-row">
	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--primary">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
				<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
			</svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo esc_html( $ppmstr_site_on ? __( 'ON', 'privex-password-protect' ) : __( 'OFF', 'privex-password-protect' ) ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Site Protection', 'privex-password-protect' ); ?></span>
		</div>
	</div>

	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--success">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
				<rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/>
			</svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_protected_count ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Protected Pages / Posts', 'privex-password-protect' ); ?></span>
		</div>
	</div>

	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--info">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
				<rect x="3" y="3" width="18" height="18" rx="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="9" y1="21" x2="9" y2="9"/>
			</svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo esc_html( ucfirst( $ppmstr_settings['active_template'] ) ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Active Template', 'privex-password-protect' ); ?></span>
		</div>
	</div>

	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--warning">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
				<path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/>
			</svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_cat_count ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Protected Categories', 'privex-password-protect' ); ?></span>
		</div>
	</div>
</div>

<!-- Quick Actions -->
<div class="ppmstr-card">
	<div class="ppmstr-card-header">
		<h3><?php esc_html_e( 'Quick Actions', 'privex-password-protect' ); ?></h3>
	</div>
	<div class="ppmstr-card-body">
		<div class="ppmstr-quick-actions">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=ppmstr-settings' ) ); ?>" class="ppmstr-quick-btn">
				<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>
				<?php esc_html_e( 'General Settings', 'privex-password-protect' ); ?>
			</a>
			<a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=page' ) ); ?>" class="ppmstr-quick-btn">
				<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 5v14M5 12h14"/></svg>
				<?php esc_html_e( 'Create Protected Page', 'privex-password-protect' ); ?>
			</a>
		</div>
	</div>
</div>

<!-- Getting Started -->
<div class="ppmstr-card">
	<div class="ppmstr-card-header">
		<h3><?php esc_html_e( 'Getting Started', 'privex-password-protect' ); ?></h3>
	</div>
	<div class="ppmstr-card-body">
		<div class="ppmstr-steps">
			<div class="ppmstr-step">
				<span class="ppmstr-step-number">1</span>
				<div class="ppmstr-step-text">
					<strong><?php esc_html_e( 'Enable Site-Wide Protection', 'privex-password-protect' ); ?></strong>
					<p><?php esc_html_e( 'Go to Settings and toggle site-wide protection to lock your entire site with a single password.', 'privex-password-protect' ); ?></p>
				</div>
			</div>
			<div class="ppmstr-step">
				<span class="ppmstr-step-number">2</span>
				<div class="ppmstr-step-text">
					<strong><?php esc_html_e( 'Protect Individual Content', 'privex-password-protect' ); ?></strong>
					<p><?php esc_html_e( 'Edit any page or post and use the Password Protection metabox to set a password.', 'privex-password-protect' ); ?></p>
				</div>
			</div>
			<div class="ppmstr-step">
				<span class="ppmstr-step-number">3</span>
				<div class="ppmstr-step-text">
					<strong><?php esc_html_e( 'Customize Your Form', 'privex-password-protect' ); ?></strong>
					<p><?php esc_html_e( 'Personalize the password form text, choose a template, and adjust cookie settings.', 'privex-password-protect' ); ?></p>
				</div>
			</div>
		</div>
	</div>
</div>
