<?php

declare( strict_types=1 );

namespace LolaCore\Actions\Handlers;

use LolaCore\Actions\Contracts\ActionHandlerInterface;

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

class SeoHandler implements ActionHandlerInterface {

	public function getSupportedActions(): array {
		return array( 'diagnose_seo' );
	}

	public function handle( string $actionId, array $params, bool $dryRun ): array {
		return match ( $actionId ) {
			'diagnose_seo' => $this->diagnoseSeo( $params ),
			default        => throw new \Exception( esc_html( "SeoHandler: unknown action '{$actionId}'." ) ),
		};
	}

	private function diagnoseSeo( array $params ): array {
		$is_public = get_option( 'blog_public' );
		$active_plugins = get_option( 'active_plugins', array() );
		$seo_plugins = array();
		
		$known_seo = array( 'wordpress-seo/wp-seo.php', 'seo-by-rank-math/rank-math.php', 'all-in-one-seo-pack/all_in_one_seo_pack.php' );
		foreach ( $active_plugins as $plugin ) {
			if ( in_array( $plugin, $known_seo, true ) ) {
				$seo_plugins[] = $plugin;
			}
		}

		return array(
			'search_engine_visibility' => (bool) $is_public,
			'seo_plugins_active'       => $seo_plugins,
			'permalink_structure'      => get_option( 'permalink_structure' ),
		);
	}
}
