<?php

/**
 * SEO integration for AI Magic plugin
 *
 * @link       https://insya.com/ai-magic
 * @since      1.0.7
 *
 * @package    Ai_Magic
 * @subpackage Ai_Magic/admin
 */

/**
 * Handles SEO plugin integrations
 *
 * @package    Ai_Magic
 * @subpackage Ai_Magic/admin
 * @author     AI Magic <info@insya.com>
 */
class Ai_Magic_SEO
{
    /**
     * Detect which SEO plugin is active
     *
     * @since    1.0.7
     * @return   string|false    Plugin name or false if none detected
     */
    public static function detect_seo_plugin()
    {
        if (defined('WPSEO_VERSION')) {
            return 'yoast';
        } elseif (defined('RANK_MATH_VERSION')) {
            return 'rankmath';
        } elseif (defined('AIOSEO_VERSION')) {
            return 'aioseo';
        }
        return false;
    }

    /**
     * Update SEO title for a post
     *
     * @since    2.0.0
     * @param    int       $post_id    Post ID
     * @param    string    $title      SEO title text
     * @return   bool                  Success status
     */
    public static function update_seo_title($post_id, $title)
    {
        $seo_plugin = self::detect_seo_plugin();

        switch ($seo_plugin) {
            case 'yoast':
                return update_post_meta($post_id, '_yoast_wpseo_title', $title);

            case 'rankmath':
                return update_post_meta($post_id, 'rank_math_title', $title);

            case 'aioseo':
                return update_post_meta($post_id, '_aioseo_title', $title);

            default:
                // Fallback to custom meta field
                return update_post_meta($post_id, 'ai_magic_seo_title', $title);
        }
    }

    /**
     * Update meta description for a post
     *
     * @since    1.0.7
     * @param    int       $post_id          Post ID
     * @param    string    $meta_description Meta description text
     * @return   bool                        Success status
     */
    public static function update_meta_description($post_id, $meta_description)
    {
        $seo_plugin = self::detect_seo_plugin();

        switch ($seo_plugin) {
            case 'yoast':
                return update_post_meta($post_id, '_yoast_wpseo_metadesc', $meta_description);

            case 'rankmath':
                return update_post_meta($post_id, 'rank_math_description', $meta_description);

            case 'aioseo':
                return update_post_meta($post_id, '_aioseo_description', $meta_description);

            default:
                // Fallback to custom meta field
                return update_post_meta($post_id, 'ai_magic_meta_description', $meta_description);
        }
    }

    /**
     * Update focus keyword for a post
     *
     * @since    1.0.7
     * @param    int       $post_id  Post ID
     * @param    string    $keyword  Focus keyword
     * @return   bool                Success status
     */
    public static function update_focus_keyword($post_id, $keyword)
    {
        $seo_plugin = self::detect_seo_plugin();

        switch ($seo_plugin) {
            case 'yoast':
                return update_post_meta($post_id, '_yoast_wpseo_focuskw', $keyword);

            case 'rankmath':
                return update_post_meta($post_id, 'rank_math_focus_keyword', $keyword);

            case 'aioseo':
                $keywords = get_post_meta($post_id, '_aioseo_keywords', true);
                if (empty($keywords)) {
                    $keywords = array();
                }
                if (!in_array($keyword, $keywords)) {
                    $keywords[] = $keyword;
                }
                return update_post_meta($post_id, '_aioseo_keywords', $keywords);

            default:
                return update_post_meta($post_id, 'ai_magic_focus_keyword', $keyword);
        }
    }

    /**
     * Get meta description for a post
     *
     * @since    1.0.7
     * @param    int    $post_id  Post ID
     * @return   string           Meta description
     */
    public static function get_meta_description($post_id)
    {
        $seo_plugin = self::detect_seo_plugin();

        switch ($seo_plugin) {
            case 'yoast':
                return get_post_meta($post_id, '_yoast_wpseo_metadesc', true);

            case 'rankmath':
                return get_post_meta($post_id, 'rank_math_description', true);

            case 'aioseo':
                return get_post_meta($post_id, '_aioseo_description', true);

            default:
                return get_post_meta($post_id, 'ai_magic_meta_description', true);
        }
    }

    /**
     * Get active SEO plugin name for display
     *
     * @since    1.0.7
     * @return   string    Plugin display name
     */
    public static function get_seo_plugin_name()
    {
        $plugin = self::detect_seo_plugin();

        switch ($plugin) {
            case 'yoast':
                return 'Yoast SEO';
            case 'rankmath':
                return 'Rank Math';
            case 'aioseo':
                return 'All in One SEO';
            default:
                return 'None (using AI Magic meta fields)';
        }
    }
}
