<?php

/**
 * Provide a admin area view for the meta tags settings
 *
 * @link       https://yourwebsite.com
 * @since      1.0.0
 *
 * @package    Basic_SEO_Helper
 * @subpackage Basic_SEO_Helper/admin/partials
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variables are scoped to this included file, not global scope

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

// Handle form submission
if ( isset( $_POST['submit'] ) && isset( $_POST['basic_seo_helper_meta_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['basic_seo_helper_meta_nonce'] ) ), 'basic_seo_helper_meta_settings' ) ) {
	// Meta tag settings
	$meta_settings = array(
		'cspgbase_enable_open_graph'    => isset( $_POST['cspgbase_enable_open_graph'] ) ? '1' : '0',
		'cspgbase_enable_twitter_cards' => isset( $_POST['cspgbase_enable_twitter_cards'] ) ? '1' : '0',
		'cspgbase_enable_canonical'     => isset( $_POST['cspgbase_enable_canonical'] ) ? '1' : '0',
		'cspgbase_enable_author_meta'   => isset( $_POST['cspgbase_enable_author_meta'] ) ? '1' : '0',
		'cspgbase_enable_robots_meta'   => isset( $_POST['cspgbase_enable_robots_meta'] ) ? '1' : '0',
	);

	// Update meta tag settings
	foreach ( $meta_settings as $option_name => $option_value ) {
		update_option( $option_name, $option_value );
	}

	echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Meta tag settings saved successfully!', 'cspg-basic-seo-helper' ) . '</p></div>';
}
?>

<div class="wrap">
	<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
	
	<div class="basic-seo-helper-admin-wrapper">
		<!-- Meta Tag Settings -->
		<div class="postbox">
			<div class="postbox-header">
				<h2><?php esc_html_e( 'Meta Tag Settings', 'cspg-basic-seo-helper' ); ?></h2>
			</div>
			<div class="inside">
				<form method="post" action="">
					<?php wp_nonce_field( 'basic_seo_helper_meta_settings', 'basic_seo_helper_meta_nonce' ); ?>
					
					<p><?php esc_html_e( 'Control which types of meta tags are generated by the plugin. Disable any meta tag types that you don\'t want included in your pages.', 'cspg-basic-seo-helper' ); ?></p>
					
					<table class="form-table" role="presentation">
						<tbody>
							<tr>
								<th scope="row"><?php esc_html_e( 'Social Media Meta Tags', 'cspg-basic-seo-helper' ); ?></th>
								<td>
									<fieldset>
										<legend class="screen-reader-text"><span><?php esc_html_e( 'Social Media Meta Tags', 'cspg-basic-seo-helper' ); ?></span></legend>
										<label for="cspgbase_enable_open_graph">
											<input name="cspgbase_enable_open_graph" type="checkbox" id="cspgbase_enable_open_graph" value="1" <?php checked( get_option( 'cspgbase_enable_open_graph', '1' ), '1' ); ?> />
											<?php esc_html_e( 'Enable Open Graph meta tags (Facebook, LinkedIn, etc.)', 'cspg-basic-seo-helper' ); ?>
										</label><br>
										<label for="cspgbase_enable_twitter_cards">
											<input name="cspgbase_enable_twitter_cards" type="checkbox" id="cspgbase_enable_twitter_cards" value="1" <?php checked( get_option( 'cspgbase_enable_twitter_cards', '1' ), '1' ); ?> />
											<?php esc_html_e( 'Enable Twitter Card meta tags', 'cspg-basic-seo-helper' ); ?>
										</label>
									</fieldset>
									<p class="description"><?php esc_html_e( 'Controls social media sharing tags that appear when your content is shared on platforms like Facebook, Twitter, and LinkedIn.', 'cspg-basic-seo-helper' ); ?></p>
								</td>
							</tr>
							
							<tr>
								<th scope="row"><?php esc_html_e( 'Additional SEO Tags', 'cspg-basic-seo-helper' ); ?></th>
								<td>
									<fieldset>
										<legend class="screen-reader-text"><span><?php esc_html_e( 'Additional SEO Tags', 'cspg-basic-seo-helper' ); ?></span></legend>
										<label for="cspgbase_enable_canonical">
											<input name="cspgbase_enable_canonical" type="checkbox" id="cspgbase_enable_canonical" value="1" <?php checked( get_option( 'cspgbase_enable_canonical', '1' ), '1' ); ?> />
											<?php esc_html_e( 'Enable canonical URL tags', 'cspg-basic-seo-helper' ); ?>
										</label><br>
										<label for="cspgbase_enable_author_meta">
											<input name="cspgbase_enable_author_meta" type="checkbox" id="cspgbase_enable_author_meta" value="1" <?php checked( get_option( 'cspgbase_enable_author_meta', '1' ), '1' ); ?> />
											<?php esc_html_e( 'Enable author meta tags', 'cspg-basic-seo-helper' ); ?>
										</label><br>
										<label for="cspgbase_enable_robots_meta">
											<input name="cspgbase_enable_robots_meta" type="checkbox" id="cspgbase_enable_robots_meta" value="1" <?php checked( get_option( 'cspgbase_enable_robots_meta', '1' ), '1' ); ?> />
											<?php esc_html_e( 'Enable robots meta tags', 'cspg-basic-seo-helper' ); ?>
										</label>
									</fieldset>
									<p class="description"><?php esc_html_e( 'Additional SEO meta tags that help search engines better understand your content structure, authorship, and crawling preferences.', 'cspg-basic-seo-helper' ); ?></p>
								</td>
							</tr>
						</tbody>
					</table>
					
					<?php submit_button( __( 'Save Meta Tag Settings', 'cspg-basic-seo-helper' ) ); ?>
				</form>
			</div>
		</div>

		<!-- Meta Tags Guide -->
		<div class="bsh-seo-elements-guide">
			<div class="bsh-guide-wrapper">
				<div class="bsh-guide-header">
					<h2><span class="bsh-guide-icon">🏷️</span><?php esc_html_e( 'Meta Tags Guide', 'cspg-basic-seo-helper' ); ?></h2>
					<p><?php esc_html_e( 'Meta tags are snippets of code placed in your site\'s head section that tell search engines and social platforms how to interpret and display your content. All essential meta tags are generated automatically based on your post title, excerpt, and featured image.', 'cspg-basic-seo-helper' ); ?></p>
				</div>
				
				<div class="bsh-guide-columns">
					<div class="bsh-guide-column">
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🌐</span><?php esc_html_e( 'Meta Tags Overview', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'What They Are:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Meta tags are snippets of code placed in your site\'s &lt;head&gt; section that tell search engines and social platforms how to interpret and display your content.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Why They Matter:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Correctly configured meta tags improve visibility in search results and ensure your links look professional when shared on platforms like Facebook, LinkedIn, and Twitter (X).', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'All essential meta tags are generated automatically based on your post title, excerpt, and featured image. You can override them in the plugin\'s settings or individual post options.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🧩</span><?php esc_html_e( 'Open Graph Tags (Facebook, LinkedIn, Slack, etc.)', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Purpose:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Open Graph (OG) tags control how your content appears when shared on social media platforms like Facebook, LinkedIn, and Slack.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Tags Generated:', 'cspg-basic-seo-helper' ); ?></strong></p>
							<table class="widefat" style="font-size: 12px; margin: 10px 0;">
								<thead>
									<tr>
										<th><strong><?php esc_html_e( 'Tag', 'cspg-basic-seo-helper' ); ?></strong></th>
										<th><strong><?php esc_html_e( 'Description', 'cspg-basic-seo-helper' ); ?></strong></th>
										<th><strong><?php esc_html_e( 'Best Practice', 'cspg-basic-seo-helper' ); ?></strong></th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td><code>og:title</code></td>
										<td><?php esc_html_e( 'The post or page title', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Keep under 90 characters; make it clear and brand-aligned', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>og:description</code></td>
										<td><?php esc_html_e( 'A short summary or excerpt', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Aim for 110–200 characters; match your meta description if possible', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>og:image</code></td>
										<td><?php esc_html_e( 'The preview image', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Use a 1200×630 px image (or larger) in JPG or PNG', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>og:url</code></td>
										<td><?php esc_html_e( 'Canonical page URL', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Always use the absolute URL (with https)', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>og:type</code></td>
										<td><?php esc_html_e( 'Defines content type (article, website)', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Posts = article, Pages = website', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>og:site_name</code></td>
										<td><?php esc_html_e( 'Your website\'s name', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Keep consistent across your entire site', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>fb:app_id</code></td>
										<td><?php esc_html_e( 'Facebook App ID (optional)', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Add only if your brand uses the Facebook API', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
								</tbody>
							</table>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'The plugin uses your post data and featured image to populate all OG fields. If no featured image is found, it automatically falls back to your default "social share image" set in plugin settings.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🐦</span><?php esc_html_e( 'Twitter Card Tags', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Purpose:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Twitter Card meta tags (now for X) determine how your links are displayed when shared on the platform.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Tags Generated:', 'cspg-basic-seo-helper' ); ?></strong></p>
							<table class="widefat" style="font-size: 12px; margin: 10px 0;">
								<thead>
									<tr>
										<th><strong><?php esc_html_e( 'Tag', 'cspg-basic-seo-helper' ); ?></strong></th>
										<th><strong><?php esc_html_e( 'Description', 'cspg-basic-seo-helper' ); ?></strong></th>
										<th><strong><?php esc_html_e( 'Best Practice', 'cspg-basic-seo-helper' ); ?></strong></th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td><code>twitter:card</code></td>
										<td><?php esc_html_e( 'Defines card type (usually summary_large_image)', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Use summary_large_image for maximum visibility', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>twitter:title</code></td>
										<td><?php esc_html_e( 'Page or post title', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Keep under 70 characters for clean display', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>twitter:description</code></td>
										<td><?php esc_html_e( 'Short content summary', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Limit to 100–200 characters; concise, engaging tone', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>twitter:image</code></td>
										<td><?php esc_html_e( 'The preview image', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Use same as og:image; optimal 1200×675 px', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>twitter:site</code></td>
										<td><?php esc_html_e( 'Your brand\'s Twitter handle', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Include if configured in plugin settings (@username)', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
								</tbody>
							</table>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'If you don\'t specify Twitter tags, the plugin automatically mirrors your OG data to ensure consistent presentation across all networks.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
					</div>
					
					<div class="bsh-guide-column">
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🔍</span><?php esc_html_e( 'Additional SEO Meta Tags', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Purpose:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'These tags enhance how search engines interpret and rank your content.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Tags Generated:', 'cspg-basic-seo-helper' ); ?></strong></p>
							<table class="widefat" style="font-size: 12px; margin: 10px 0;">
								<thead>
									<tr>
										<th><strong><?php esc_html_e( 'Tag', 'cspg-basic-seo-helper' ); ?></strong></th>
										<th><strong><?php esc_html_e( 'Description', 'cspg-basic-seo-helper' ); ?></strong></th>
										<th><strong><?php esc_html_e( 'Best Practice', 'cspg-basic-seo-helper' ); ?></strong></th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td><code>description</code></td>
										<td><?php esc_html_e( 'Standard meta description for search results', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Keep 150-160 characters for optimal display in search results', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>robots</code></td>
										<td><?php esc_html_e( 'Controls indexing and crawling behavior', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Default: index, follow. Use noindex for private or test pages', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>canonical</code></td>
										<td><?php esc_html_e( 'Defines the preferred version of a page', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Ensures Google indexes only one URL version (avoid duplicates)', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>author</code></td>
										<td><?php esc_html_e( 'Links to the author\'s profile page', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Include for blog posts or editorial content', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
									<tr>
										<td><code>og:locale</code></td>
										<td><?php esc_html_e( 'Specifies the language and region for content', 'cspg-basic-seo-helper' ); ?></td>
										<td><?php esc_html_e( 'Auto-detected from WordPress, ensures international clarity', 'cspg-basic-seo-helper' ); ?></td>
									</tr>
								</tbody>
							</table>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'All additional tags are generated automatically, using your WordPress post and author metadata. You can customize robots behavior per-post in the SEO meta box.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🧱</span><?php esc_html_e( 'Example Meta Tags Output', 'cspg-basic-seo-helper' ); ?></h4>
							<textarea readonly rows="14" cols="50" class="large-text code" style="font-family: monospace; font-size: 11px;">&lt;!-- Standard Meta Tags --&gt;
&lt;meta name="description" content="Your page description..."&gt;

&lt;!-- Open Graph / Facebook --&gt;
&lt;meta property="og:type" content="article"&gt;
&lt;meta property="og:title" content="Your Page Title"&gt;
&lt;meta property="og:description" content="Your page description..."&gt;
&lt;meta property="og:image" content="https://yoursite.com/image.jpg"&gt;
&lt;meta property="og:url" content="https://yoursite.com/page-url"&gt;
&lt;meta property="og:site_name" content="Your Site Name"&gt;
&lt;meta property="og:locale" content="en_US"&gt;

&lt;!-- Twitter --&gt;
&lt;meta name="twitter:card" content="summary_large_image"&gt;
&lt;meta name="twitter:title" content="Your Page Title"&gt;
&lt;meta name="twitter:description" content="Your page description..."&gt;
&lt;meta name="twitter:image" content="https://yoursite.com/image.jpg"&gt;

&lt;!-- Additional SEO --&gt;
&lt;link rel="canonical" href="https://yoursite.com/page-url"&gt;</textarea>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">⚙️</span><?php esc_html_e( 'How Basic SEO Helper Handles Meta Tags', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Automatic Processing:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Automatically updates when content, featured image, or titles change', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Respects per-post overrides and global templates', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Ensures no duplicate or conflicting tags', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Validates output format for SEO compliance', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Uses defaults when specific data is missing (e.g., fallback social image)', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">💡</span><?php esc_html_e( 'Pro Tip: Validate Your Meta Tags', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Use these free tools to test your meta data:', 'cspg-basic-seo-helper' ); ?></strong></p>
							
							<p><strong>🧠 <?php esc_html_e( 'Facebook Sharing Debugger:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<a href="https://developers.facebook.com/tools/debug/" target="_blank">https://developers.facebook.com/tools/debug/</a></p>
							
							<p><strong>🐦 <?php esc_html_e( 'Twitter Card Validator:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<a href="https://cards-dev.twitter.com/validator" target="_blank">https://cards-dev.twitter.com/validator</a></p>
							
							<p><strong>🔍 <?php esc_html_e( 'Google Rich Results Test:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<a href="https://search.google.com/test/rich-results" target="_blank">https://search.google.com/test/rich-results</a></p>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
