<?php

/**
 * Schedule Tab Template — Automatic Speed Tests
 *
 * Gated to Personal plan and higher via Freemius SDK.
 * Free users see a grayed-out preview with upgrade CTA.
 *
 * Frequency limits per plan:
 *   - Personal: Monthly, Weekly
 *   - Pro:      Monthly, Weekly, Daily
 *   - Agency:   Monthly, Weekly, Daily, Hourly
 *
 * @package SpeedInChina
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
global $speeinch_fs;
$current_tab = 'schedule';
$speeinch_can_use_cloud = function_exists( 'speeinch_can_collect_data' ) && speeinch_can_collect_data();
$speeinch_is_connecting = function_exists( 'speeinch_is_connecting' ) && speeinch_is_connecting();
$speeinch_admin_email = get_bloginfo( 'admin_email' );
$speeinch_is_personal = $speeinch_can_use_cloud && $speeinch_fs->is_plan( 'personal', true );
$speeinch_is_pro = $speeinch_can_use_cloud && $speeinch_fs->is_plan( 'pro', true );
$speeinch_is_agency = $speeinch_can_use_cloud && $speeinch_fs->is_plan( 'agency', true );
$speeinch_is_paid = $speeinch_is_personal || $speeinch_is_pro || $speeinch_is_agency;
?>
<div class="wrap speeinch-wrap">
	<h1><?php 
esc_html_e( 'Speed in China', 'speed-in-china' );
?></h1>

	<?php 
require_once SPEEINCH_PLUGIN_PATH . 'admin/templates/nav-tabs.php';
?>

<?php 
if ( $speeinch_is_connecting ) {
    ?>
	<?php 
    /* Pending email-confirmation state. R3 still applies — outbound calls remain gated. */
    ?>
	<div class="speeinch_container speeinch-empty-state speeinch-empty-state--pending">
		<h2><?php 
    esc_html_e( '📧 Almost there — check your inbox', 'speed-in-china' );
    ?></h2>
		<p style="max-width:640px;">
			<?php 
    printf( 
        /* translators: %s: admin email address */
        esc_html__( 'We sent a confirmation email to %s. Click the link in that email to finish connecting and unlock scheduled speed tests.', 'speed-in-china' ),
        '<strong>' . esc_html( $speeinch_admin_email ) . '</strong>'
     );
    ?>
		</p>
		<p style="max-width:640px;" class="description">
			<?php 
    esc_html_e( 'Local features (Google Fonts localization and Resource Analysis) are already active and do not require email confirmation.', 'speed-in-china' );
    ?>
		</p>
		<p>
			<a class="button" href="<?php 
    echo esc_url( ( function_exists( 'speeinch_cancel_pending_url' ) ? speeinch_cancel_pending_url() : admin_url( 'plugins.php' ) ) );
    ?>">
				<?php 
    esc_html_e( 'Cancel and use anonymous mode', 'speed-in-china' );
    ?>
			</a>
		</p>
	</div>
<?php 
} elseif ( !$speeinch_can_use_cloud ) {
    ?>
	<div class="speeinch_container speeinch-empty-state speeinch-empty-state--cloud">
		<h2><?php 
    esc_html_e( '🗓 Connect to enable scheduled tests', 'speed-in-china' );
    ?></h2>
		<p style="max-width:640px;">
			<?php 
    esc_html_e( 'Schedule automatic China-side speed tests on a recurring cadence. Tests run from a Globalping probe in mainland China; connecting shares your site URL and minimal technical data.', 'speed-in-china' );
    ?>
		</p>
		<p>
			<a class="button button-primary button-hero" href="<?php 
    echo esc_url( ( function_exists( 'speeinch_reconnect_url' ) ? speeinch_reconnect_url() : admin_url( 'plugins.php' ) ) );
    ?>">
				<?php 
    esc_html_e( 'Connect', 'speed-in-china' );
    ?>
			</a>
		</p>
		<p class="description" style="max-width:640px;">
			<?php 
    printf( 
        /* translators: %s: privacy policy URL */
        esc_html__( 'Read our %s before connecting. Google Fonts localization and Resource Analysis work without connecting.', 'speed-in-china' ),
        '<a href="https://speedinchinaplugin.com/terms-conditions-privacy-policy" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Privacy Policy &amp; Terms', 'speed-in-china' ) . '</a>'
     );
    ?>
		</p>
	</div>
<?php 
} elseif ( !($speeinch_is_pro || $speeinch_is_agency) ) {
    ?>
	<!-- FREE USER: Grayed-out scheduling with upgrade CTA -->
	<div class="speeinch_container speeinch-upsell-card" style="text-align:center;">
		<div class="speeinch-lock-icon">🔒</div>
		<h2><?php 
    esc_html_e( '🗓 Schedule Automatic Speed Tests', 'speed-in-china' );
    ?></h2>
		<p><?php 
    esc_html_e( 'Stay up to date with your site\'s performance in China. Schedule automatic speed tests so you never miss a performance change.', 'speed-in-china' );
    ?></p>

		<!-- Grayed-out schedule preview -->
		<div class="speeinch-grayed-preview">
			<div style="opacity: 0.35; pointer-events: none; max-width: 500px; margin: 20px auto; padding: 25px; border: 1px solid #c3c4c7; border-radius: 6px; background: #f8f9fa; text-align: left;">
				<p style="margin:0 0 8px;"><strong><?php 
    esc_html_e( 'Test Frequency', 'speed-in-china' );
    ?></strong></p>
				<select disabled style="width:100%;margin-bottom:15px;padding:6px;">
					<option><?php 
    esc_html_e( 'Weekly', 'speed-in-china' );
    ?></option>
				</select>
				<p style="margin:0 0 8px;"><strong><?php 
    esc_html_e( 'Status', 'speed-in-china' );
    ?></strong></p>
				<label style="display:flex;align-items:center;gap:8px;margin-bottom:15px;">
					<input type="checkbox" disabled checked />
					<?php 
    esc_html_e( 'Enable Scheduled Tests', 'speed-in-china' );
    ?>
				</label>
				<p style="margin:0 0 8px;"><strong><?php 
    esc_html_e( 'Next Scheduled Test', 'speed-in-china' );
    ?></strong></p>
				<p style="color:#666;">March 10, 2026 09:00 AM</p>
				<button class="button button-primary" disabled><?php 
    esc_html_e( 'Save Schedule', 'speed-in-china' );
    ?></button>
			</div>
		</div>

		<div class="speeinch-upgrade-cta">
			<p><strong><?php 
    esc_html_e( 'Upgrade to Pro to unlock scheduled speed tests', 'speed-in-china' );
    ?></strong></p>
			<ul class="speeinch-feature-list">
				<li>✅ <?php 
    esc_html_e( 'Pro: Schedule Monthly, Weekly, and Daily tests', 'speed-in-china' );
    ?></li>
				<li>✅ <?php 
    esc_html_e( 'Agency: Add Hourly automatic monitoring', 'speed-in-china' );
    ?></li>
			</ul>
			<a href="<?php 
    echo esc_url( $speeinch_fs->get_upgrade_url() );
    ?>" class="button button-primary button-hero">
				<?php 
    esc_html_e( '⬆️ Upgrade to Pro', 'speed-in-china' );
    ?>
			</a>
		</div>
	</div>

	<?php 
    // is__premium_only
    ?>

<?php 
}
// ! ( $speeinch_is_pro || $speeinch_is_agency )
?>

	<!--Features-->
	<?php 
require_once SPEEINCH_PLUGIN_PATH . 'admin/templates/features.php';
?>
</div>
