<?php

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

interface AISBW_SEO_Adapter_Interface
{
    public function save(int $post_id, array $article): void;
}

final class AISBW_Default_Meta_Adapter implements AISBW_SEO_Adapter_Interface
{
    public function save(int $post_id, array $article): void
    {
        update_post_meta($post_id, '_aisbw_meta_title', sanitize_text_field((string) ($article['meta_title'] ?? '')));
        update_post_meta($post_id, '_aisbw_meta_description', sanitize_textarea_field((string) ($article['meta_description'] ?? '')));
    }
}

final class AISBW_SEO_Adapter_Factory
{
    public static function make(): AISBW_SEO_Adapter_Interface
    {
        if ( function_exists( 'aisbw_fs' ) && aisbw_fs()->can_use_premium_code__premium_only() ) {
            $premium_file = AISBW_PLUGIN_DIR . 'includes/class-seo-premium.php';
            if ( file_exists( $premium_file ) ) {
                require_once $premium_file;
            }

            if (class_exists('AISBW_Yoast_SEO_Adapter') && defined('WPSEO_VERSION')) {
                return new AISBW_Yoast_SEO_Adapter();
            }
            if (class_exists('AISBW_Rank_Math_SEO_Adapter') && defined('RANK_MATH_VERSION')) {
                return new AISBW_Rank_Math_SEO_Adapter();
            }
        }

        return new AISBW_Default_Meta_Adapter();
    }
}
