<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$first_post_type = ! empty( $post_types ) ? array_key_first( $post_types ) : 'post';
$wordai_textdomain = SftcyWordaiSeoScan::get_textdomain();
$seo_integration = SftcyWordaiSeoIntegrationManager::status();
?>

<?php include SFTCY_WORDAI_PLUGIN_DIR . 'admin/views/settings-pages-header-template.php'; ?>

<div class="wrap sc-wordai-settings-page sc-wordai-seo-scan-page">
	<div class="sc-wordai-seo-scan-hero">
		<div>
			<span class="sc-wordai-seo-scan-eyebrow"><?php esc_html_e( 'SEO Scan', $wordai_textdomain ); ?></span>
			<h1><?php esc_html_e( 'Scan, score, and improve WordPress content SEO', $wordai_textdomain ); ?></h1>
			<p><?php esc_html_e( 'Review posts, pages, and products with practical SEO checks, clear suggestions, and AI-assisted metadata fixes.', $wordai_textdomain ); ?></p>
		</div>
		<div class="sc-wordai-seo-usage-card" data-wordai-seo-usage>
			<?php if ( ! empty( $usage['limited'] ) ) : ?>
				<span class="sc-wordai-seo-usage-label"><?php esc_html_e( 'Free monthly usage', $wordai_textdomain ); ?></span>
				<strong>
					<span data-wordai-usage-used><?php echo esc_html( (string) $usage['used'] ); ?></span>/<span data-wordai-usage-limit><?php echo esc_html( (string) $usage['limit'] ); ?></span>
				</strong>
				<small><?php echo esc_html( $usage['month_label'] ); ?></small>
				<a href="<?php echo esc_url( $usage['upgrade_url'] ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Upgrade for unlimited scans', $wordai_textdomain ); ?></a>
			<?php else : ?>
				<span class="sc-wordai-seo-usage-label"><?php esc_html_e( 'Pro access', $wordai_textdomain ); ?></span>
				<strong><?php esc_html_e( 'Unlimited', $wordai_textdomain ); ?></strong>
				<small><?php esc_html_e( 'SEO scans and AI auto-fixes', $wordai_textdomain ); ?></small>
			<?php endif; ?>
		</div>
	</div>

	<div class="sc-wordai-seo-integration-card <?php echo ! empty( $seo_integration['conflict'] ) ? 'has-conflict' : ''; ?>" data-wordai-seo-integration>
		<div class="sc-wordai-seo-integration-icon"><i class="fa-solid fa-shield-halved" aria-hidden="true"></i></div>
		<div class="sc-wordai-seo-integration-copy">
			<span class="sc-wordai-seo-integration-label"><?php esc_html_e( 'SEO Integration', $wordai_textdomain ); ?></span>
			<strong data-wordai-seo-owner-name>
				<?php
				echo esc_html(
					! empty( $seo_integration['conflict'] )
						? __( 'Owner selection required', $wordai_textdomain )
						: sprintf( __( 'Publishing through %s', $wordai_textdomain ), $seo_integration['selected_name'] )
				);
				?>
			</strong>
			<p data-wordai-seo-owner-detail>
				<?php
				if ( ! empty( $seo_integration['conflict'] ) ) {
					esc_html_e( 'Multiple supported SEO plugins are active. Select one owner before WordAI can apply SEO changes.', $wordai_textdomain );
				} elseif ( 'native' === $seo_integration['selected'] ) {
					esc_html_e( 'No supported external SEO plugin is active. WordAI safely manages frontend SEO metadata.', $wordai_textdomain );
				} else {
					echo esc_html( sprintf( __( 'WordAI reads and writes SEO fields through %1$s %2$s and suppresses duplicate WordAI output.', $wordai_textdomain ), $seo_integration['selected_name'], $seo_integration['selected_version'] ) );
				}
				?>
			</p>
		</div>
		<?php if ( count( $seo_integration['active'] ) > 1 ) : ?>
			<div class="sc-wordai-seo-owner-control">
				<label for="sc-wordai-seo-owner"><?php esc_html_e( 'SEO output owner', $wordai_textdomain ); ?></label>
				<select id="sc-wordai-seo-owner">
					<option value="auto" <?php selected( $seo_integration['mode'], 'auto' ); ?>><?php esc_html_e( 'Choose owner', $wordai_textdomain ); ?></option>
					<?php foreach ( $seo_integration['active'] as $provider ) : ?>
						<option value="<?php echo esc_attr( $provider['id'] ); ?>" <?php selected( $seo_integration['mode'], $provider['id'] ); ?>><?php echo esc_html( $provider['label'] . ' ' . $provider['version'] ); ?></option>
					<?php endforeach; ?>
				</select>
				<button type="button" class="button button-primary sc-wordai-seo-owner-save"><?php esc_html_e( 'Use this owner', $wordai_textdomain ); ?></button>
			</div>
		<?php else : ?>
			<span class="sc-wordai-seo-owner-state is-ready"><i class="fa-solid fa-circle-check" aria-hidden="true"></i><?php esc_html_e( 'Ready', $wordai_textdomain ); ?></span>
		<?php endif; ?>
	</div>

	<div class="sc-wordai-seo-tabs" role="tablist" aria-label="<?php esc_attr_e( 'Content type tabs', $wordai_textdomain ); ?>">
		<?php foreach ( $post_types as $type_key => $type_data ) : ?>
			<button
				type="button"
				class="sc-wordai-seo-tab <?php echo $type_key === $first_post_type ? 'is-active' : ''; ?>"
				data-post-type="<?php echo esc_attr( $type_key ); ?>"
			>
				<?php echo esc_html( $type_data['label'] ); ?>
			</button>
		<?php endforeach; ?>
	</div>

	<div class="sc-wordai-seo-toolbar">
		<div class="sc-wordai-seo-search">
			<i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
			<input type="search" id="sc-wordai-seo-search" placeholder="<?php esc_attr_e( 'Search title...', $wordai_textdomain ); ?>" />
		</div>
		<select id="sc-wordai-seo-post-status" aria-label="<?php esc_attr_e( 'Post status', $wordai_textdomain ); ?>">
			<option value="publish"><?php esc_html_e( 'Published', $wordai_textdomain ); ?></option>
			<option value="draft"><?php esc_html_e( 'Drafts', $wordai_textdomain ); ?></option>
			<option value="pending"><?php esc_html_e( 'Pending', $wordai_textdomain ); ?></option>
			<option value="private"><?php esc_html_e( 'Private', $wordai_textdomain ); ?></option>
			<option value="future"><?php esc_html_e( 'Scheduled', $wordai_textdomain ); ?></option>
		</select>
		<select id="sc-wordai-seo-filter" aria-label="<?php esc_attr_e( 'SEO status', $wordai_textdomain ); ?>">
			<option value="all"><?php esc_html_e( 'All SEO states', $wordai_textdomain ); ?></option>
			<option value="unscanned"><?php esc_html_e( 'Scan required', $wordai_textdomain ); ?></option>
			<option value="recheck"><?php esc_html_e( 'Re-scan required', $wordai_textdomain ); ?></option>
			<option value="needs-work"><?php esc_html_e( 'Needs work', $wordai_textdomain ); ?></option>
		</select>
		<button type="button" class="button sc-wordai-seo-refresh">
			<i class="fa-solid fa-rotate" aria-hidden="true"></i>
			<?php esc_html_e( 'Refresh', $wordai_textdomain ); ?>
		</button>
		<button type="button" class="button button-primary sc-wordai-seo-schedule-open">
			<i class="fa-solid fa-calendar-check" aria-hidden="true"></i>
			<?php esc_html_e( 'Scheduled Bulk Scan', $wordai_textdomain ); ?>
		</button>
	</div>

	<div class="sc-wordai-seo-schedule-summary" data-wordai-seo-job-summary hidden></div>
	<div class="sc-wordai-seo-feedback" data-wordai-seo-feedback hidden></div>

	<div class="sc-wordai-seo-table-card">
		<div class="sc-wordai-seo-table-head">
			<div>
				<h2 data-wordai-seo-current-label><?php echo esc_html( $post_types[ $first_post_type ]['label'] ?? __( 'Posts', $wordai_textdomain ) ); ?></h2>
				<p><?php esc_html_e( 'Rows are loaded with pagination so large content libraries stay responsive.', $wordai_textdomain ); ?></p>
			</div>
			<div class="sc-wordai-seo-page-size">
				<label for="sc-wordai-seo-per-page"><?php esc_html_e( 'Rows', $wordai_textdomain ); ?></label>
				<select id="sc-wordai-seo-per-page">
					<option value="20">20</option>
					<option value="30">30</option>
					<option value="50">50</option>
				</select>
			</div>
		</div>

		<div class="sc-wordai-seo-table-wrap">
			<table class="widefat fixed striped sc-wordai-seo-table">
				<thead>
					<tr>
						<th class="column-title"><?php esc_html_e( 'Content item', $wordai_textdomain ); ?></th>
						<th><?php esc_html_e( 'SEO status', $wordai_textdomain ); ?></th>
						<th><?php esc_html_e( 'Score', $wordai_textdomain ); ?></th>
						<th><?php esc_html_e( 'Focus keyword', $wordai_textdomain ); ?></th>
						<th><?php esc_html_e( 'Updated', $wordai_textdomain ); ?></th>
						<th class="column-actions"><?php esc_html_e( 'Actions', $wordai_textdomain ); ?></th>
					</tr>
				</thead>
				<tbody data-wordai-seo-list>
					<tr>
						<td colspan="6" class="sc-wordai-seo-loading"><?php esc_html_e( 'Loading SEO items...', $wordai_textdomain ); ?></td>
					</tr>
				</tbody>
			</table>
		</div>

		<div class="sc-wordai-seo-pagination">
			<button type="button" class="button" data-wordai-page-prev disabled>
				<i class="fa-solid fa-chevron-left" aria-hidden="true"></i>
				<?php esc_html_e( 'Previous', $wordai_textdomain ); ?>
			</button>
			<span data-wordai-page-label><?php esc_html_e( 'Page 1', $wordai_textdomain ); ?></span>
			<button type="button" class="button" data-wordai-page-next disabled>
				<?php esc_html_e( 'Next', $wordai_textdomain ); ?>
				<i class="fa-solid fa-chevron-right" aria-hidden="true"></i>
			</button>
		</div>
	</div>

	<div id="sc-wordai-seo-schedule-modal" class="sc-wordai-seo-schedule-modal" title="<?php esc_attr_e( 'Schedule Bulk SEO Scan', $wordai_textdomain ); ?>" style="display:none;">
		<div class="sc-wordai-seo-schedule-intro">
			<i class="fa-solid fa-calendar-check" aria-hidden="true"></i>
			<div>
				<strong><?php esc_html_e( 'Run SEO scanning in the background', $wordai_textdomain ); ?></strong>
				<p><?php esc_html_e( 'Choose a task, content scope, and start time. WordAI processes published items in small batches so large sites stay responsive.', $wordai_textdomain ); ?></p>
			</div>
		</div>

		<div class="sc-wordai-seo-schedule-form">
			<label>
				<span><?php esc_html_e( 'Task', $wordai_textdomain ); ?></span>
				<select id="sc-wordai-seo-schedule-task">
					<option value="scan"><?php esc_html_e( 'Scan', $wordai_textdomain ); ?></option>
					<option value="scan_autofix"><?php esc_html_e( 'Scan and AI Auto Fix', $wordai_textdomain ); ?></option>
				</select>
			</label>
			<label>
				<span><?php esc_html_e( 'Content scope', $wordai_textdomain ); ?></span>
				<select id="sc-wordai-seo-schedule-scope">
					<option value="unscanned_recheck"><?php esc_html_e( 'Scan required and re-scan required items', $wordai_textdomain ); ?></option>
					<option value="needs_work"><?php esc_html_e( 'Items with SEO score below 75', $wordai_textdomain ); ?></option>
					<option value="all"><?php esc_html_e( 'All published items in selected types', $wordai_textdomain ); ?></option>
				</select>
			</label>
			<label>
				<span><?php esc_html_e( 'Scheduled date and time', $wordai_textdomain ); ?></span>
				<input type="datetime-local" id="sc-wordai-seo-schedule-time" />
			</label>
			<fieldset>
				<legend><?php esc_html_e( 'Content types', $wordai_textdomain ); ?></legend>
				<div class="sc-wordai-seo-schedule-types">
					<?php foreach ( $post_types as $type_key => $type_data ) : ?>
						<label>
							<input type="checkbox" name="sc-wordai-seo-schedule-post-types" value="<?php echo esc_attr( $type_key ); ?>" checked />
							<span><?php echo esc_html( $type_data['label'] ); ?></span>
						</label>
					<?php endforeach; ?>
				</div>
			</fieldset>
		</div>

		<?php if ( ! empty( $usage['limited'] ) ) : ?>
			<div class="sc-wordai-seo-schedule-upgrade">
				<i class="fa-solid fa-lock" aria-hidden="true"></i>
				<span><?php esc_html_e( 'Scheduled bulk SEO scanning is a Pro feature. Upgrade to unlock background scan and AI Auto Fix jobs for your content library.', $wordai_textdomain ); ?></span>
				<a href="<?php echo esc_url( $usage['upgrade_url'] ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Upgrade to Pro', $wordai_textdomain ); ?></a>
			</div>
		<?php endif; ?>
	</div>

	<div id="sc-wordai-seo-proposal-modal" class="sc-wordai-seo-proposal-modal" title="<?php esc_attr_e( 'Review Proposed SEO Changes', $wordai_textdomain ); ?>" style="display:none;"></div>
</div>
