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

namespace SEOvaultSEO\Migration\Providers;

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

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

class SlimSEO extends BaseImporter {

	const PLUGIN_NAME = 'Slim SEO';
	const PLUGIN_FILE = 'slim-seo/slim-seo.php';
	const META_PREFIX = 'slim_seo_';

	const EXCLUDED_KEYS = array();

	const NOT_ALLOWED_TYPES = array(
		'elementor_library',
	);

	const GENERAL_MAPPING = array(
		'slim_seo_title'         => 'seo_title',
		'slim_seo_description'   => 'seo_description',
		'slim_seo_canonical_url' => 'canonical_url',
	);

	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( 'SLIM_SEO_VER' ) || class_exists( 'SlimSEO\\Container' );
	}

	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 import_post_general_settings( $post_id ) {
		$imported = $this->process_meta_mapping( self::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 );

		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( 'slim_seo_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 ) {
		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( false, $post_id, 'post', 'social' ),
			false
		);
	}

	public function import_term_social( $term_id ) {
		return ImporterUtils::build_response(
			ImporterUtils::import_status_message( false, $term_id, 'term', 'social' ),
			false
		);
	}

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