<?php
/**
 * WBAG RankMath - SEO Plugin Integration
 *
 * Handles integration with various SEO plugins including RankMath,
 * Yoast SEO, and SEOPress.
 *
 * @since 1.0.0
 * @package WP_Blog_Agent
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class WBAG_RankMath
 *
 * Handles SEO plugin integration for the WP Blog Agent plugin.
 *
 * @since 1.0.0
 */
class WBAG_RankMath {

	/**
	 * Sets SEO meta fields based on the configured SEO plugin.
	 *
	 * This static method updates post meta fields according to the
	 * selected SEO plugin's conventions. It supports RankMath, Yoast,
	 * SEOPress, or none.
	 *
	 * @since 1.0.0
	 *
	 * @param int   $post_id   The post ID to update.
	 * @param array $seo_meta  Array containing SEO metadata:
	 *                          - 'focus_keyword' (string)
	 *                          - 'meta_description' (string)
	 *                          - 'meta_title' (string).
	 * @param array $settings  Array containing 'seo_plugin' key.
	 *
	 * @return bool True on success, false on failure.
	 */
	public static function set_seo_meta( $post_id, $seo_meta, $settings ) {
		// Validate inputs.
		if ( ! is_int( $post_id ) || $post_id < 1 ) {
			return false;
		}

		if ( ! is_array( $seo_meta ) || ! is_array( $settings ) ) {
			return false;
		}

		$plugin = isset( $settings['seo_plugin'] ) ? $settings['seo_plugin'] : 'none';

		switch ( $plugin ) {
			case 'rankmath':
				update_post_meta( $post_id, 'rank_math_focus_keyword', isset( $seo_meta['focus_keyword'] ) ? $seo_meta['focus_keyword'] : '' );
				update_post_meta( $post_id, 'rank_math_description', isset( $seo_meta['meta_description'] ) ? $seo_meta['meta_description'] : '' );
				update_post_meta( $post_id, 'rank_math_title', isset( $seo_meta['meta_title'] ) ? $seo_meta['meta_title'] : '' );
				break;

			case 'yoast':
				update_post_meta( $post_id, '_yoast_wpseo_focuskw', isset( $seo_meta['focus_keyword'] ) ? $seo_meta['focus_keyword'] : '' );
				update_post_meta( $post_id, '_yoast_wpseo_metadesc', isset( $seo_meta['meta_description'] ) ? $seo_meta['meta_description'] : '' );
				update_post_meta( $post_id, '_yoast_wpseo_title', isset( $seo_meta['meta_title'] ) ? $seo_meta['meta_title'] : '' );
				break;

			case 'seopress':
				update_post_meta( $post_id, '_seopress_titles_title', isset( $seo_meta['meta_title'] ) ? $seo_meta['meta_title'] : '' );
				update_post_meta( $post_id, '_seopress_titles_desc', isset( $seo_meta['meta_description'] ) ? $seo_meta['meta_description'] : '' );
				update_post_meta( $post_id, '_seopress_analysis_target_kw', isset( $seo_meta['focus_keyword'] ) ? $seo_meta['focus_keyword'] : '' );
				break;

			case 'none':
				// Do nothing for 'none'.
				break;

			default:
				return false;
		}

		return true;
	}

	/**
	 * Checks if a specific SEO plugin is active.
	 *
	 * Determines whether the specified SEO plugin is currently active
	 * in the WordPress installation.
	 *
	 * @since 1.0.0
	 *
	 * @param string $plugin The plugin to check: 'rankmath', 'yoast', or 'seopress'.
	 *
	 * @return bool True if the plugin is active, false otherwise.
	 */
	public static function is_seo_plugin_active( $plugin ) {
		switch ( $plugin ) {
			case 'rankmath':
				return class_exists( 'RankMath' ) || function_exists( 'rank_math' );

			case 'yoast':
				return defined( 'WPSEO_VERSION' );

			case 'seopress':
				return defined( 'SEOPRESS_VERSION' );

			default:
				return false;
		}
	}

	/**
	 * Retrieves SEO score information from the active plugin.
	 *
	 * Attempts to gather available SEO analysis or score information
	 * from the specified SEO plugin for a given post.
	 *
	 * @since 1.0.0
	 *
	 * @param int    $post_id The post ID to retrieve score information for.
	 * @param string $plugin  The SEO plugin to query.
	 *
	 * @return array Array containing available SEO score info, or empty array if unavailable.
	 */
	public static function get_seo_score_info( $post_id, $plugin ) {
		if ( ! is_int( $post_id ) || $post_id < 1 ) {
			return array();
		}

		$info = array();

		switch ( $plugin ) {
			case 'rankmath':
				if ( class_exists( 'RankMath' ) ) {
					// Try to get RankMath SEO score.
					$score = get_post_meta( $post_id, 'rank_math_score', true );
					if ( $score ) {
						$info['score'] = $score;
					}

					// Try to get focus keyword analysis.
					$focus_keyword = get_post_meta( $post_id, 'rank_math_focus_keyword', true );
					if ( $focus_keyword ) {
						$info['focus_keyword'] = $focus_keyword;
					}
				}
				break;

			case 'yoast':
				if ( defined( 'WPSEO_VERSION' ) ) {
					// Try to get Yoast readability score.
					$readability = get_post_meta( $post_id, '_yoast_wpseo_content_score', true );
					if ( $readability ) {
						$info['readability_score'] = $readability;
					}

					// Try to get focus keyword.
					$focus_keyword = get_post_meta( $post_id, '_yoast_wpseo_focuskw', true );
					if ( $focus_keyword ) {
						$info['focus_keyword'] = $focus_keyword;
					}
				}
				break;

			case 'seopress':
				if ( defined( 'SEOPRESS_VERSION' ) ) {
					// Try to get SEOPress analysis data.
					$analysis = get_post_meta( $post_id, '_seopress_analysis_result', true );
					if ( $analysis ) {
						$info['analysis'] = $analysis;
					}

					// Try to get target keyword.
					$target_kw = get_post_meta( $post_id, '_seopress_analysis_target_kw', true );
					if ( $target_kw ) {
						$info['target_keyword'] = $target_kw;
					}
				}
				break;

			default:
				break;
		}

		return $info;
	}
}
