<?php

/**
 * Meta box for displaying SEO information on post/page edit screens
 *
 * @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;
}

// Get plugin options
$options = get_option( 'cspgbase_options', array() );

// Get post meta values
$custom_title       = get_post_meta( $post->ID, '_basic_seo_helper_title', true );
$custom_description = get_post_meta( $post->ID, '_basic_seo_helper_description', true );
$custom_image       = get_post_meta( $post->ID, '_basic_seo_helper_image', true );

// Get social media meta values
$og_title            = get_post_meta( $post->ID, '_basic_seo_helper_og_title', true );
$og_description      = get_post_meta( $post->ID, '_basic_seo_helper_og_description', true );
$twitter_title       = get_post_meta( $post->ID, '_basic_seo_helper_twitter_title', true );
$twitter_description = get_post_meta( $post->ID, '_basic_seo_helper_twitter_description', true );
$schema_enabled      = get_post_meta( $post->ID, '_basic_seo_helper_schema_enabled', true );
// Default based on global setting if no value is set (empty = new post, '1' = enabled, '0' = disabled)
if ( '' === $schema_enabled ) {
	$schema_enabled = $options['default_schema_enabled'] ?? '1';
}

// Get robots meta values
$noindex   = get_post_meta( $post->ID, '_basic_seo_helper_noindex', true );
$nofollow  = get_post_meta( $post->ID, '_basic_seo_helper_nofollow', true );
$noarchive = get_post_meta( $post->ID, '_basic_seo_helper_noarchive', true );
$nosnippet = get_post_meta( $post->ID, '_basic_seo_helper_nosnippet', true );

// Get current post information for preview
$title       = $custom_title ? $custom_title : get_the_title( $post->ID );
$description = $custom_description ? $custom_description : wp_trim_words( wp_strip_all_tags( $post->post_content ), 30 );
$excerpt     = $post->post_excerpt ? $post->post_excerpt : $description;

// Get image
$image = $custom_image;
if ( ! $image && has_post_thumbnail( $post->ID ) ) {
	$image = get_the_post_thumbnail_url( $post->ID, 'large' );
}
if ( ! $image && ! empty( $options['default_image'] ) ) {
	$image = $options['default_image'];
}

// Get URL
$url = get_permalink( $post->ID );
if ( ! $url ) {
	$url = home_url() . '/?p=' . $post->ID;
}
?>

<div class="basic-seo-helper-meta-box">
	<?php wp_nonce_field( 'basic_seo_helper_meta_box', 'basic_seo_helper_meta_nonce' ); ?>
	
	<div class="cspgbase-section-header">
		<h4><?php esc_html_e( 'SEO Meta Tags Preview', 'cspg-basic-seo-helper' ); ?></h4>
		<p class="description"><?php esc_html_e( 'Preview of the meta tags that will be generated for this post/page. You can customize them below.', 'cspg-basic-seo-helper' ); ?></p>
	</div>

	<!-- Custom SEO Fields -->
	<table class="form-table">
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_title"><?php esc_html_e( 'Custom SEO Title (Meta Title)', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<input type="text" id="basic_seo_helper_title" name="basic_seo_helper_title" value="<?php echo esc_attr( $custom_title ); ?>" class="large-text" placeholder="<?php echo esc_attr( $title ); ?>" />
				<p class="description"><?php esc_html_e( 'Leave empty to use the post title automatically.', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>
		
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_description"><?php esc_html_e( 'Custom SEO Description (Meta Description)', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<textarea id="basic_seo_helper_description" name="basic_seo_helper_description" rows="3" class="large-text" placeholder="<?php echo esc_attr( $description ); ?>"><?php echo esc_textarea( $custom_description ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Leave empty to use the post excerpt or auto-generated description.', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>
		
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_image"><?php esc_html_e( 'Custom Social Image', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<input type="url" id="basic_seo_helper_image" name="basic_seo_helper_image" value="<?php echo esc_attr( $custom_image ); ?>" class="large-text" placeholder="<?php echo esc_attr( $image ); ?>" />
				<button type="button" class="button" id="upload_meta_image_button"><?php esc_html_e( 'Upload Image', 'cspg-basic-seo-helper' ); ?></button>
				<p class="description"><?php esc_html_e( 'Leave empty to use the featured image or default social image.', 'cspg-basic-seo-helper' ); ?></p>
				<?php if ( $image ) : ?>
					<div class="cspgbase-image-preview">
						<img src="<?php echo esc_url( $image ); ?>" alt="<?php esc_html_e( 'Social Image Preview', 'cspg-basic-seo-helper' ); ?>" />
					</div>
				<?php endif; ?>
			</td>
		</tr>
		
		<!-- Social Media Settings -->
		<tr>
			<th scope="row" colspan="2">
				<h4 class="cspgbase-subsection-header"><?php esc_html_e( 'Social Media Settings', 'cspg-basic-seo-helper' ); ?></h4>
				<p class="description"><?php esc_html_e( 'These fields are used for SEO scoring. Fill them out to improve your SEO score.', 'cspg-basic-seo-helper' ); ?></p>
			</th>
		</tr>
		
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_og_title"><?php esc_html_e( 'Open Graph Title', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<input type="text" id="basic_seo_helper_og_title" name="basic_seo_helper_og_title" value="<?php echo esc_attr( $og_title ); ?>" class="large-text" placeholder="<?php echo esc_attr( $title ); ?>" />
				<p class="description"><?php esc_html_e( 'Title for Facebook and other social platforms (15 points toward SEO score).', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>
		
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_og_description"><?php esc_html_e( 'Open Graph Description', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<textarea id="basic_seo_helper_og_description" name="basic_seo_helper_og_description" rows="3" class="large-text" placeholder="<?php echo esc_attr( $description ); ?>"><?php echo esc_textarea( $og_description ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Description for Facebook and other social platforms (required for 15 points).', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>
		
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_twitter_title"><?php esc_html_e( 'Twitter Card Title', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<input type="text" id="basic_seo_helper_twitter_title" name="basic_seo_helper_twitter_title" value="<?php echo esc_attr( $twitter_title ); ?>" class="large-text" placeholder="<?php echo esc_attr( $title ); ?>" />
				<p class="description"><?php esc_html_e( 'Title for Twitter cards (15 points toward SEO score).', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>
		
		<tr>
			<th scope="row">
				<label for="basic_seo_helper_twitter_description"><?php esc_html_e( 'Twitter Card Description', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<textarea id="basic_seo_helper_twitter_description" name="basic_seo_helper_twitter_description" rows="3" class="large-text" placeholder="<?php echo esc_attr( $description ); ?>"><?php echo esc_textarea( $twitter_description ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Description for Twitter cards (required for 15 points).', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>
		
		<!-- Schema Settings -->
		<tr>
			<th scope="row" colspan="2">
				<h4 class="cspgbase-subsection-header"><?php esc_html_e( 'Schema Markup Settings', 'cspg-basic-seo-helper' ); ?></h4>
			</th>
		</tr>

		<tr>
			<th scope="row">
				<label for="basic_seo_helper_schema_enabled"><?php esc_html_e( 'Enable Schema Markup', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<input type="checkbox" id="basic_seo_helper_schema_enabled" name="basic_seo_helper_schema_enabled" value="1" <?php checked( $schema_enabled, '1' ); ?> />
				<label for="basic_seo_helper_schema_enabled"><?php esc_html_e( 'Enable structured data for this post/page (10 points toward SEO score)', 'cspg-basic-seo-helper' ); ?></label>
				<p class="description"><?php esc_html_e( 'Schema markup helps search engines understand your content better.', 'cspg-basic-seo-helper' ); ?></p>
			</td>
		</tr>

		<!-- Robots Meta Tag Settings -->
		<tr>
			<th scope="row" colspan="2">
				<h4 class="cspgbase-subsection-header"><?php esc_html_e( 'Search Engine Indexing (Robots)', 'cspg-basic-seo-helper' ); ?></h4>
				<p class="description"><?php esc_html_e( 'Control how search engines index and display this page. By default, all pages are indexed and followed.', 'cspg-basic-seo-helper' ); ?></p>
			</th>
		</tr>

		<tr>
			<th scope="row">
				<label><?php esc_html_e( 'Robots Directives', 'cspg-basic-seo-helper' ); ?></label>
			</th>
			<td>
				<fieldset>
					<legend class="screen-reader-text"><span><?php esc_html_e( 'Robots Directives', 'cspg-basic-seo-helper' ); ?></span></legend>
					<label for="basic_seo_helper_noindex">
						<input type="checkbox" id="basic_seo_helper_noindex" name="basic_seo_helper_noindex" value="1" <?php checked( $noindex, '1' ); ?> />
						<?php esc_html_e( 'No Index - Prevent search engines from indexing this page', 'cspg-basic-seo-helper' ); ?>
					</label><br>
					<label for="basic_seo_helper_nofollow">
						<input type="checkbox" id="basic_seo_helper_nofollow" name="basic_seo_helper_nofollow" value="1" <?php checked( $nofollow, '1' ); ?> />
						<?php esc_html_e( 'No Follow - Prevent search engines from following links on this page', 'cspg-basic-seo-helper' ); ?>
					</label><br>
					<label for="basic_seo_helper_noarchive">
						<input type="checkbox" id="basic_seo_helper_noarchive" name="basic_seo_helper_noarchive" value="1" <?php checked( $noarchive, '1' ); ?> />
						<?php esc_html_e( 'No Archive - Prevent search engines from showing a cached version', 'cspg-basic-seo-helper' ); ?>
					</label><br>
					<label for="basic_seo_helper_nosnippet">
						<input type="checkbox" id="basic_seo_helper_nosnippet" name="basic_seo_helper_nosnippet" value="1" <?php checked( $nosnippet, '1' ); ?> />
						<?php esc_html_e( 'No Snippet - Prevent search engines from showing a text snippet in results', 'cspg-basic-seo-helper' ); ?>
					</label>
				</fieldset>
			</td>
		</tr>

	</table>

	<!-- Meta Tags Preview -->
	<div class="cspgbase-meta-preview">
		<h4><?php esc_html_e( 'Generated Meta Tags Preview:', 'cspg-basic-seo-helper' ); ?></h4>
		
		<div class="cspgbase-preview-section">
			<strong><?php esc_html_e( 'Open Graph (Facebook, LinkedIn):', 'cspg-basic-seo-helper' ); ?></strong>
			<pre class="cspgbase-preview-code"><code>&lt;meta property="og:title" content="<?php echo esc_html( $title ); ?>"&gt;
&lt;meta property="og:description" content="<?php echo esc_html( $description ); ?>"&gt;
&lt;meta property="og:image" content="<?php echo esc_html( $image ); ?>"&gt;
&lt;meta property="og:url" content="<?php echo esc_html( $url ); ?>"&gt;
&lt;meta property="og:type" content="<?php echo esc_html( 'post' === $post->post_type ? 'article' : 'website' ); ?>"&gt;
&lt;meta property="og:site_name" content="<?php echo esc_html( get_bloginfo( 'name' ) ); ?>"&gt;</code></pre>
		</div>

		<div class="cspgbase-preview-section">
			<strong><?php esc_html_e( 'Twitter Cards:', 'cspg-basic-seo-helper' ); ?></strong>
			<pre class="cspgbase-preview-code"><code>&lt;meta name="twitter:card" content="summary_large_image"&gt;
&lt;meta name="twitter:title" content="<?php echo esc_html( $title ); ?>"&gt;
&lt;meta name="twitter:description" content="<?php echo esc_html( $description ); ?>"&gt;
&lt;meta name="twitter:image" content="<?php echo esc_html( $image ); ?>"&gt;<?php if ( ! empty( $options['twitter_username'] ) ) : ?>
&lt;meta name="twitter:site" content="<?php echo esc_html( $options['twitter_username'] ); ?>"&gt;
	<?php
										endif;
										?>
										</code></pre>
		</div>

		<div>
			<strong><?php esc_html_e( 'Schema.org Structured Data:', 'cspg-basic-seo-helper' ); ?></strong>
			<pre class="cspgbase-preview-code"><code>{
	"@context": "https://schema.org",
	"@type": "<?php echo esc_js( 'post' === $post->post_type ? 'Article' : 'WebPage' ); ?>",
	"headline": "<?php echo esc_js( $title ); ?>",
	"description": "<?php echo esc_js( $description ); ?>",
	"url": "<?php echo esc_js( $url ); ?>"
					<?php
					if ( $image ) :
						?>
		,
	"image": {
	"@type": "ImageObject",
	"url": "<?php echo esc_js( $image ); ?>"
	}
						<?php endif; ?>
				<?php
				if ( 'post' === $post->post_type ) :
					?>
		,
	"datePublished": "<?php echo esc_js( get_the_date( 'c', $post->ID ) ); ?>",
	"dateModified": "<?php echo esc_js( get_the_modified_date( 'c', $post->ID ) ); ?>",
	"author": {
	"@type": "Person",
	"name": "<?php echo esc_js( get_the_author_meta( 'display_name', $post->post_author ) ); ?>"
	}
				<?php endif; ?>
}</code></pre>
		</div>
	</div>

	<div class="cspgbase-social-preview-container">
		<strong><?php esc_html_e( 'Social Media Preview:', 'cspg-basic-seo-helper' ); ?></strong>
		<div class="cspgbase-social-preview-card">
			<?php if ( $image ) : ?>
				<img src="<?php echo esc_url( $image ); ?>" alt="" />
			<?php endif; ?>
			<div class="cspgbase-social-preview-content">
				<div class="cspgbase-social-preview-title">
					<?php echo esc_html( $title ); ?>
				</div>
				<div class="cspgbase-social-preview-description">
					<?php echo esc_html( wp_trim_words( $description, 20 ) ); ?>
				</div>
				<div class="cspgbase-social-preview-url">
					<?php echo esc_html( wp_parse_url( $url, PHP_URL_HOST ) ); ?>
				</div>
			</div>
		</div>
	</div>
</div>

