<?php
/**
 * The SEO Framework Importer
 *
 * Imports SEO meta from The SEO Framework into SEOvault.
 *
 * @package SEOvaultSEO\Migration\Providers
 */

namespace SEOvaultSEO\Migration\Providers;

use SEOvaultSEO\Migration\BaseImporter;
use SEOvaultSEO\Migration\ImporterUtils;

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

class TheSeoFramework extends BaseImporter {

	const PLUGIN_NAME = 'The SEO Framework';
	const PLUGIN_FILE = 'autodescription/autodescription.php';
	const META_PREFIX = '_genesis_';

	const EXCLUDED_KEYS = array();

	const NOT_ALLOWED_TYPES = array(
		'elementor_library',
	);

	const GENERAL_MAPPING = array(
		'_genesis_title'       => 'seo_title',
		'_genesis_description' => 'seo_description',
		'_genesis_canonical'   => 'canonical_url',
	);

	const TSF_GENERAL_MAPPING = array(
		'seo_title'       => 'seo_title',
		'seo_description' => 'seo_description',
		'canonical_url'   => 'canonical_url',
	);

	const SOCIAL_MAPPING = array(
		'_social_image_url'       => 'og_image',
		'_open_graph_title'       => 'og_title',
		'_open_graph_description' => 'og_description',
	);

	public function get_plugin_name() {
		return self::PLUGIN_NAME;
	}

	public function get_plugin_file() {
		return self::PLUGIN_FILE;
	}

	public function is_plugin_active() {
		return defined( 'THE_SEO_FRAMEWORK_VERSION' ) || defined( 'TSF_VERSION' );
	}

	protected function get_meta_key_prefix() {
		return self::META_PREFIX;
	}

	protected function get_excluded_meta_keys() {
		return self::EXCLUDED_KEYS;
	}

	protected function get_not_allowed_types() {
		return self::NOT_ALLOWED_TYPES;
	}

	protected function get_source_meta_data( $id, $is_taxonomy, $type = '' ) {
		if ( $is_taxonomy ) {
			return get_term_meta( $id );
		}
		return get_post_meta( $id );
	}

	public function detect_post( $post_id ) {
		$meta  = get_post_meta( $post_id );
		$found = false;

		foreach ( array_keys( $meta ) as $key ) {
			if ( str_starts_with( $key, '_genesis_' ) || str_starts_with( $key, 'seo_' ) || str_starts_with( $key, '_social_' ) || str_starts_with( $key, '_open_graph_' ) ) {
				if ( ! in_array( $key, array( 'seo_title', 'seo_description' ), true ) || get_post_type( $post_id ) ) {
					$found = true;
					break;
				}
			}
		}

		if ( $found ) {
			return ImporterUtils::build_response(
				sprintf( /* translators: 1: plugin name, 2: post ID */ __( '%1$s data detected for post %2$d.', 'seovault-ai' ), $this->get_plugin_name(), $post_id ),
				true
			);
		}

		ImporterUtils::update_migration_flag( $post_id );
		return ImporterUtils::build_response(
			sprintf( /* translators: 1: plugin name, 2: post ID */ __( 'No %1$s data found for post %2$d.', 'seovault-ai' ), $this->get_plugin_name(), $post_id ),
			false,
			array(),
			true
		);
	}

	public function import_post_general_settings( $post_id ) {
		$imported = $this->process_meta_mapping( self::GENERAL_MAPPING );
		if ( ! $imported ) {
			$imported = $this->process_meta_mapping( self::TSF_GENERAL_MAPPING );
		}

		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( $imported, $post_id, 'post', 'general' ),
			$imported
		);
	}

	public function import_term_general_settings( $term_id ) {
		$imported = $this->process_meta_mapping( self::GENERAL_MAPPING );
		if ( ! $imported ) {
			$imported = $this->process_meta_mapping( self::TSF_GENERAL_MAPPING );
		}

		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( $imported, $term_id, 'term', 'general' ),
			$imported
		);
	}

	public function import_post_meta_robots( $post_id ) {
		$noindex  = $this->get_source( 'noindex', '' );
		$imported = false;

		if ( '1' === $noindex ) {
			$this->target_meta['robots_noindex'] = true;
			$imported                            = true;
		}

		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( $imported, $post_id, 'post', 'meta_robots' ),
			$imported
		);
	}

	public function import_term_meta_robots( $term_id ) {
		return $this->import_post_meta_robots( $term_id );
	}

	public function import_post_social( $post_id ) {
		$imported = $this->process_meta_mapping( self::SOCIAL_MAPPING );

		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( $imported, $post_id, 'post', 'social' ),
			$imported
		);
	}

	public function import_term_social( $term_id ) {
		$imported = $this->process_meta_mapping( self::SOCIAL_MAPPING );

		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( $imported, $term_id, 'term', 'social' ),
			$imported
		);
	}

	public function import_global_settings() {
		return ImporterUtils::build_response(
			__( 'No global settings found to import from The SEO Framework.', 'seovault-ai' ),
			false
		);
	}
}
