<?php
/**
 * WordAI SEO Metabox Template
 * Handles SEO title, meta description, focus keyword, canonical, robots, schema, and meta tags toggle.
 */

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

global $post;

// Detect if editing or creating new post
$is_edit_mode      = isset( $post->ID ) && $post->ID > 0;
$current_permalink = $is_edit_mode ? get_permalink( $post->ID ) : '';

// Retrieve stored meta fields
$seo_document = SftcyWordaiSeoIntegrationManager::read( $post->ID );
if ( is_wp_error( $seo_document ) ) {
	$seo_document = SftcyWordaiSeoIntegrationManager::providers()['native']->read( $post->ID );
}
$seo_title            = $seo_document['title'];
$seo_meta_description = $seo_document['description'];
$seo_focus_keyword    = $seo_document['focus_keyword'];
$seo_canonical_url    = $seo_document['canonical'];
$seo_robots_meta      = $seo_document['robots'];
$seo_schema_type      = $seo_document['schema_type'];
$seo_integration      = SftcyWordaiSeoIntegrationManager::status();

// Fallback canonical URL for edit mode
if ( empty( $seo_canonical_url ) && $is_edit_mode && ! empty( $current_permalink ) ) {
	$seo_canonical_url = $current_permalink;
}
?>

<div class="wordai-seo-metabox-wrapper">
	<div class="wordai-seo-header">
		<div class="wordai-seo-header-copy">
			<strong><span class="dashicons dashicons-chart-line"></span> <?php esc_html_e( 'WordAI SEO Settings', 'wordai-pro' ); ?></strong>
			<span class="wordai-seo-owner-badge <?php echo ! empty( $seo_integration['conflict'] ) ? 'has-conflict' : ''; ?>">
				<i class="dashicons <?php echo ! empty( $seo_integration['conflict'] ) ? 'dashicons-warning' : 'dashicons-shield-alt'; ?>" aria-hidden="true"></i>
				<?php
				echo esc_html(
					! empty( $seo_integration['conflict'] )
						? __( 'Select SEO owner in SEO Scan', 'wordai-pro' )
						: sprintf( __( 'Publishing through %s', 'wordai-pro' ), $seo_integration['selected_name'] )
				);
				?>
			</span>
		</div>
		<button type="button" class="toggle-seo-panel" aria-expanded="true">
			<span class="dashicons dashicons-arrow-up-alt2"></span>
		</button>
	</div>

	<div class="wordai-seo-body">

		<!-- 🔹 SEO Title -->
		<div class="wordai-seo-field">
			<div class="wordai-seo-field-top">
				<label for="wordaiSEOTitle"><strong><?php esc_html_e( 'SEO Title', 'wordai-pro' ); ?></strong></label>
				<span class="wordai-seo-rank-circle" data-type="title">0%</span>
			</div>

			<textarea id="wordaiSEOTitle" name="wordaiseotitle" rows="2" maxlength="60"
				placeholder="<?php esc_attr_e( 'Enter SEO title…', 'wordai-pro' ); ?>"><?php echo esc_textarea( $seo_title ); ?></textarea>
			<p class="wordai-seo-char-counter" data-type="title">0 / 60</p>

			<div class="wordai-seo-hint">
				<?php esc_html_e( 'Recommended: 50-60 characters. Include your focus keyword near the beginning.', 'wordai-pro' ); ?>
			</div>
		</div>

		<!-- 🔹 SEO Meta Description -->
		<div class="wordai-seo-field">
			<div class="wordai-seo-field-top">
				<label for="wordaiSEOMetaDescription"><strong><?php esc_html_e( 'SEO Meta Description', 'wordai-pro' ); ?></strong></label>
				<span class="wordai-seo-rank-circle" data-type="description">0%</span>
			</div>

			<textarea id="wordaiSEOMetaDescription" name="wordaiseometadescription" rows="3" maxlength="160"
				placeholder="<?php esc_attr_e( 'Write SEO meta description…', 'wordai-pro' ); ?>"><?php echo esc_textarea( $seo_meta_description ); ?></textarea>
			<p class="wordai-seo-char-counter" data-type="description">0 / 160</p>

			<div class="wordai-seo-hint">
				<?php esc_html_e( 'Keep under 160 characters. Summarize your post and use active voice.', 'wordai-pro' ); ?>
			</div>
		</div>

		<!-- 🔹 Focus Keyword -->
		<div class="wordai-seo-field">
			<div class="wordai-seo-field-top">
				<label for="wordaiSEOFocusKeyword"><strong><?php esc_html_e( 'Focus Keyword', 'wordai-pro' ); ?></strong></label>
				<span class="wordai-seo-rank-circle" data-type="keyword">0%</span>
			</div>

			<input type="text" id="wordaiSEOFocusKeyword" name="wordaiseofocuskeyword"
				value="<?php echo esc_attr( $seo_focus_keyword ); ?>"
				placeholder="<?php esc_attr_e( 'Primary keyword for content…', 'wordai-pro' ); ?>">
			<div class="wordai-seo-hint">
				<?php esc_html_e( 'Single main keyword or keyphrase. Avoid stuffing multiple.', 'wordai-pro' ); ?>
			</div>
		</div>

		<!-- 🔹 Canonical URL -->
		<div class="wordai-seo-field">
			<label for="wordaiSEOCannonical"><strong><?php esc_html_e( 'Canonical URL', 'wordai-pro' ); ?></strong></label>
			<input type="url" id="wordaiSEOCannonical" name="wordaiscanonicalurl"
				value="<?php echo esc_url( $seo_canonical_url ); ?>"
				placeholder="<?php esc_attr_e( 'https://example.com/your-post-seo-title/', 'wordai-pro' ); ?>">
			<div class="wordai-seo-hint">
				<?php esc_html_e( 'Canonical URL prevents duplicate content issues. Usually your main page URL.', 'wordai-pro' ); ?>
			</div>
		</div>

		<!-- 🔹 Robots Meta -->
		<div class="wordai-seo-field">
			<label for="wordaiSEORobots"><strong><?php esc_html_e( 'Robots Meta', 'wordai-pro' ); ?></strong></label>
			<select id="wordaiSEORobots" name="wordairobotsmeta">
				<option value="index,follow" <?php selected( $seo_robots_meta, 'index,follow' ); ?>>index, follow</option>
				<option value="noindex,follow" <?php selected( $seo_robots_meta, 'noindex,follow' ); ?>>noindex, follow</option>
				<option value="index,nofollow" <?php selected( $seo_robots_meta, 'index,nofollow' ); ?>>index, nofollow</option>
				<option value="noindex,nofollow" <?php selected( $seo_robots_meta, 'noindex,nofollow' ); ?>>noindex, nofollow</option>
			</select>
			<div class="wordai-seo-hint">
				<?php esc_html_e( 'Control indexing and link-following behavior for search engines.', 'wordai-pro' ); ?>
			</div>
		</div>

		<!-- 🔹 Schema Type -->
		<div class="wordai-seo-field">
			<label for="wordaiSEOSchemaType"><strong><?php esc_html_e( 'Schema Type (JSON-LD)', 'wordai-pro' ); ?></strong></label>
			<select id="wordaiSEOSchemaType" name="wordaischema">
				<option value="Article" <?php selected( $seo_schema_type, 'Article' ); ?>>Article</option>
				<option value="NewsArticle" <?php selected( $seo_schema_type, 'NewsArticle' ); ?>>NewsArticle</option>
				<option value="BlogPosting" <?php selected( $seo_schema_type, 'BlogPosting' ); ?>>BlogPosting</option>
				<option value="Product" <?php selected( $seo_schema_type, 'Product' ); ?>>Product</option>
				<option value="Organization" <?php selected( $seo_schema_type, 'Organization' ); ?>>Organization</option>
				<option value="FAQPage" <?php selected( $seo_schema_type, 'FAQPage' ); ?>>FAQPage</option>
			</select>
			<div class="wordai-seo-hint">
				<?php esc_html_e( 'Select Schema type for structured JSON-LD markup.', 'wordai-pro' ); ?>
			</div>
		</div>

		<!-- 🔹 Enable SEO Social Meta Tags -->
		<?php		
		$current_id = isset( $post->ID ) ? (int) $post->ID : 0;
		?>
		<div class="wordai-seo-toggle">
			<label>
				<input type="checkbox" id="sc-wordai-enable-social-meta"
					<?php checked( true ); // default checked in UI ?>
				/>
				<?php esc_html_e( 'Enable SEO Social Meta Tags', 'wordai-pro' ); ?>
			</label>
			<small class="description">
				<?php esc_html_e( 'When enabled, Open Graph & Twitter tags will be added for this content.', 'wordai-pro' ); ?>
			</small>
			<input type="hidden" id="sc-wordai-current-post-id" value="<?php echo esc_attr( $current_id ); ?>" />
		</div>
	    <p class="wordai-seo-meta-tags-status-msg"></p>


		<!-- 🔹 Google SERP Preview -->
		<div class="wordai-seo-serp-preview">
			<h4><span class="dashicons dashicons-google"></span> <?php esc_html_e( 'Google Search Preview', 'wordai-pro' ); ?></h4>
			<div class="serp-view-toggle">
				<button type="button" class="active" data-view="desktop">
					<span class="dashicons dashicons-desktop"></span> <?php esc_html_e( 'Desktop', 'wordai-pro' ); ?>
				</button>
				<button type="button" data-view="mobile">
					<span class="dashicons dashicons-smartphone"></span> <?php esc_html_e( 'Mobile', 'wordai-pro' ); ?>
				</button>
			</div>

			<div class="serp-preview-box">
				<div class="serp-title" id="wordaiSerpTitle">
					<?php echo esc_html( $seo_title ?: get_the_title( $post->ID ) ); ?>
				</div>
				<div class="serp-url" id="wordaiSerpUrl">
					<?php echo esc_url( $seo_canonical_url ?: $current_permalink ); ?>
				</div>
				<div class="serp-description" id="wordaiSerpDesc">
					<?php
					$desc = $seo_meta_description ?: wp_trim_words( wp_strip_all_tags( get_post_field( 'post_content', $post->ID ) ), 25 );
					echo esc_html( $desc );
					?>
				</div>
			</div>
		</div>

		<?php wp_nonce_field( 'wordai_save_seo_fields_nonce_action', 'wordai_save_seo_fields_nonce' ); ?>
	</div>
</div>
