<?php
/**
 * Partial — Settings / Advanced tab (includes uninstall modal and toggle scripts).
 *
 * @package Contentosapp_Studio
 * @since   3.2.0
 */

defined( 'ABSPATH' ) || exit;
?>
<div id="fos-tab-advanced" class="fos-tab-panel">

	<!-- Automation zone ─────────────────────────────────────────────────── -->
	<p class="fos-int-zone-label"><?php esc_html_e( 'Automation', 'contentosapp-studio' ); ?></p>
	<div class="fos-int-card">

		<div class="fos-int-card__header">
			<div class="fos-int-card__left">
				<span class="fos-int-card__icon">⚙️</span>
				<div>
					<p class="fos-int-card__title"><?php esc_html_e( 'Pipeline behavior', 'contentosapp-studio' ); ?></p>
					<p class="fos-int-card__subtitle"><?php esc_html_e( 'Fine-tune how Contentosapp Studio handles retries and external lookups during production.', 'contentosapp-studio' ); ?></p>
				</div>
			</div>
		</div>

		<div class="fos-int-card__body">

			<!-- Auto-retry failed agents -->
			<div class="fos-int-toggle-row">
				<div class="fos-int-toggle-row__info">
					<div class="fos-int-toggle-row__title"><?php esc_html_e( 'Auto-retry failed agents', 'contentosapp-studio' ); ?></div>
					<p class="fos-int-toggle-row__desc"><?php esc_html_e( 'When an AI backend returns a transient overload error, automatically reschedule the agent up to 3 times before marking the production as failed.', 'contentosapp-studio' ); ?></p>
				</div>
				<div class="fos-int-toggle-row__control">
					<label class="fos-switch fos-switch--compact">
						<input type="checkbox" id="fos-auto-retry-agents"
							<?php checked( (bool) get_option( 'costudio_auto_retry_agents', true ) ); ?>
							aria-label="<?php esc_attr_e( 'Auto-retry failed agents', 'contentosapp-studio' ); ?>">
						<span class="fos-switch__slider"></span>
					</label>
				</div>
			</div>

			<!-- Web search for Agents 01 & 03 -->
			<div class="fos-int-toggle-row">
				<div class="fos-int-toggle-row__info">
					<div class="fos-int-toggle-row__title"><?php esc_html_e( 'Web search for Discoverer & Researcher', 'contentosapp-studio' ); ?></div>
					<p class="fos-int-toggle-row__desc"><?php esc_html_e( 'Enrich Agent 01 (Discoverer) and Agent 03 (Researcher) prompts with live web search results. Disable to save API credits or skip external lookups.', 'contentosapp-studio' ); ?></p>
				</div>
				<div class="fos-int-toggle-row__control">
					<label class="fos-switch fos-switch--compact">
						<input type="checkbox" id="fos-enable-web-search"
							<?php checked( (bool) get_option( 'costudio_enable_web_search', true ) ); ?>
							aria-label="<?php esc_attr_e( 'Web search for Discoverer and Researcher', 'contentosapp-studio' ); ?>">
						<span class="fos-switch__slider"></span>
					</label>
				</div>
			</div>

			<!-- Anonymous usage telemetry -->
			<div class="fos-int-toggle-row">
				<div class="fos-int-toggle-row__info">
					<div class="fos-int-toggle-row__title"><?php esc_html_e( 'Share anonymous usage data', 'contentosapp-studio' ); ?></div>
					<p class="fos-int-toggle-row__desc">
						<?php esc_html_e( 'Helps the Contentosapp Studio team prioritize improvements. We never share your content, keywords or credentials.', 'contentosapp-studio' ); ?>
						<a href="#" id="fos-telemetry-details-link" style="color:var(--fos-primary);font-weight:500;text-decoration:none;">
							<?php esc_html_e( 'See what we send', 'contentosapp-studio' ); ?>
						</a>
					</p>
				</div>
				<div class="fos-int-toggle-row__control">
					<label class="fos-switch fos-switch--compact">
						<input type="checkbox" id="fos-telemetry-enabled"
							<?php checked( class_exists( 'Costudio_Telemetry' ) && Costudio_Telemetry::is_enabled() ); ?>
							aria-label="<?php esc_attr_e( 'Share anonymous usage data', 'contentosapp-studio' ); ?>">
						<span class="fos-switch__slider"></span>
					</label>
				</div>
			</div>

			<!-- Heavy weekly DB maintenance -->
			<div class="fos-int-toggle-row">
				<div class="fos-int-toggle-row__info">
					<div class="fos-int-toggle-row__title"><?php esc_html_e( 'Optimize database tables during weekly cleanup', 'contentosapp-studio' ); ?></div>
					<p class="fos-int-toggle-row__desc"><?php esc_html_e( 'Runs OPTIMIZE TABLE on Contentosapp Studio tables after cleanup. Keep disabled on larger sites unless your host recommends it.', 'contentosapp-studio' ); ?></p>
				</div>
				<div class="fos-int-toggle-row__control">
					<label class="fos-switch fos-switch--compact">
						<input type="checkbox" id="fos-housekeeping-optimize-tables"
							<?php checked( (bool) get_option( 'costudio_housekeeping_optimize_tables', false ) ); ?>
							aria-label="<?php esc_attr_e( 'Optimize database tables during weekly cleanup', 'contentosapp-studio' ); ?>">
						<span class="fos-switch__slider"></span>
					</label>
				</div>
			</div>

		</div><!-- /.fos-int-card__body -->
	</div><!-- /.fos-int-card -->

	<!-- ── Telemetry details modal (shared visual style with wizard + banner) ── -->
	<div id="fos-telemetry-details-modal" style="display:none;position:fixed;inset:0;z-index:100000;background:rgba(0,0,0,0.5);align-items:center;justify-content:center;">
		<div class="fos-modal-card">
			<div class="fos-modal-card__header">
				<div class="fos-modal-card__title-wrap">
					<span class="fos-modal-card__hicon">📊</span>
					<span class="fos-modal-card__title"><?php esc_html_e( 'What we collect', 'contentosapp-studio' ); ?></span>
				</div>
				<button type="button" class="fos-modal-card__close" id="fos-telemetry-details-x" aria-label="<?php esc_attr_e( 'Close', 'contentosapp-studio' ); ?>">&times;</button>
			</div>
			<div class="fos-modal-card__body">
				<div class="fos-modal-card__section fos-modal-card__section--ok">
					<p class="fos-modal-card__section-title">✓ <?php esc_html_e( 'What we send', 'contentosapp-studio' ); ?></p>
					<ul class="fos-modal-card__list">
						<li><?php esc_html_e( 'AI models used (e.g. Gemini, Claude)', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Article type (Pillar, News, etc.)', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Output language', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Tokens consumed and execution time', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Anonymous site identifier (irreversible hash)', 'contentosapp-studio' ); ?></li>
					</ul>
				</div>
				<div class="fos-modal-card__section fos-modal-card__section--no">
					<p class="fos-modal-card__section-title">✗ <?php esc_html_e( 'What we never send', 'contentosapp-studio' ); ?></p>
					<ul class="fos-modal-card__list">
						<li><?php esc_html_e( 'Article content', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Keywords or production briefings', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Credentials (API keys, passwords)', 'contentosapp-studio' ); ?></li>
						<li><?php esc_html_e( 'Real site URLs', 'contentosapp-studio' ); ?></li>
					</ul>
				</div>
			</div>
			<div class="fos-modal-card__footer">
				<button type="button" class="fos-modal-card__btn--primary" id="fos-telemetry-details-close">
					<?php esc_html_e( 'Got it', 'contentosapp-studio' ); ?>
				</button>
			</div>
		</div>
	</div>

	<!-- Danger zone ─────────────────────────────────────────────────────── -->
	<p class="fos-int-zone-label"><?php esc_html_e( 'Danger Zone', 'contentosapp-studio' ); ?></p>
	<div class="fos-int-card fos-int-card--danger">

		<div class="fos-int-card__header">
			<div class="fos-int-card__left">
				<span class="fos-int-card__icon">⚠️</span>
				<div>
					<p class="fos-int-card__title"><?php esc_html_e( 'Irreversible actions', 'contentosapp-studio' ); ?></p>
					<p class="fos-int-card__subtitle"><?php esc_html_e( 'Be careful — these options affect permanent data.', 'contentosapp-studio' ); ?></p>
				</div>
			</div>
		</div>

		<div class="fos-int-card__body">

			<div class="fos-int-toggle-row fos-int-toggle-row--danger">
				<div class="fos-int-toggle-row__info">
					<div class="fos-int-toggle-row__title"><?php esc_html_e( 'Delete all data on uninstall', 'contentosapp-studio' ); ?></div>
					<p class="fos-int-toggle-row__desc"><?php esc_html_e( 'If enabled, all Contentosapp Studio data (productions, planning, settings) will be permanently deleted when you uninstall the plugin. This action cannot be undone.', 'contentosapp-studio' ); ?></p>
				</div>
				<div class="fos-int-toggle-row__control">
					<label class="fos-switch fos-switch--compact fos-switch--danger">
						<input type="checkbox" id="fos-uninstall-delete-data"
							<?php checked( get_option( 'costudio_uninstall_delete_data', false ) ); ?>
							aria-label="<?php esc_attr_e( 'Delete all data on uninstall', 'contentosapp-studio' ); ?>">
						<span class="fos-switch__slider"></span>
					</label>
				</div>
			</div>

		</div><!-- /.fos-int-card__body -->
	</div><!-- /.fos-int-card -->

</div><!-- #fos-tab-advanced -->

<!-- ── Uninstall Confirmation Modal ──────────────────────────────────────── -->
<div id="fos-uninstall-confirm-modal" style="display:none;position:fixed;inset:0;z-index:100000;background:rgba(0,0,0,0.5);align-items:center;justify-content:center;">
	<div style="background:#fff;border-radius:12px;padding:24px;max-width:420px;width:90%;box-shadow:0 20px 60px rgba(0,0,0,0.3);">
		<h3 style="margin:0 0 12px;color:#dc2626;">⚠️ <?php esc_html_e( 'Confirm data deletion', 'contentosapp-studio' ); ?></h3>
		<p style="margin:0 0 16px;font-size:13px;color:#374151;">
			<?php esc_html_e( 'You are about to enable permanent data deletion on uninstall. All productions, planning entries, and settings will be lost forever when the plugin is removed.', 'contentosapp-studio' ); ?>
		</p>
		<p style="margin:0 0 16px;font-size:13px;color:#374151;">
			<?php esc_html_e( 'Type DELETE to confirm:', 'contentosapp-studio' ); ?>
		</p>
		<input type="text" id="fos-uninstall-confirm-input" style="width:100%;padding:8px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;box-sizing:border-box;" autocomplete="off">
		<div style="display:flex;gap:8px;margin-top:16px;justify-content:flex-end;">
			<button type="button" id="fos-uninstall-cancel-btn" class="fos-btn" style="background:#f3f4f6;color:#374151;">
				<?php esc_html_e( 'Cancel', 'contentosapp-studio' ); ?>
			</button>
			<button type="button" id="fos-uninstall-confirm-btn" class="fos-btn" style="background:#dc2626;color:#fff;opacity:0.5;pointer-events:none;" disabled>
				<?php esc_html_e( 'Enable deletion', 'contentosapp-studio' ); ?>
			</button>
		</div>
	</div>
</div>
