<?php
/**
 * Publish Mode Migrator Utility
 *
 * Handles migration from old publish mode values (publish, schedule, draft)
 * to new standardized values (immediate, scheduled, draft).
 *
 * @package Wisewand
 * @since 2.1.2
 * @license GPL-2.0-or-later
 */

namespace Wisewand\Utils;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Publish Mode Migrator
 *
 * Provides backward compatibility for publish mode values during transition
 * from old naming (publish/schedule) to new naming (immediate/scheduled).
 *
 * @package Wisewand
 * @since 2.1.2
 */
class Publish_Mode_Migrator {

	/**
	 * Value mapping from old to new
	 *
	 * @since 2.1.2
	 * @var array
	 */
	const VALUE_MAP = array(
		'publish'  => 'immediate',
		'schedule' => 'scheduled',
		'draft'    => 'draft',
	);

	/**
	 * Translate old publish mode value to new value
	 *
	 * @since 2.1.2
	 *
	 * @param string $old_value Old publish mode value.
	 * @return string New publish mode value.
	 */
	public static function translate_old_to_new( $old_value ) {
		if ( empty( $old_value ) ) {
			return 'draft';
		}

		// Already new value.
		if ( in_array( $old_value, array( 'immediate', 'scheduled', 'draft' ), true ) ) {
			return $old_value;
		}

		// Translate old value.
		if ( isset( self::VALUE_MAP[ $old_value ] ) ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( sprintf( 'Wisewand: Translating publish mode "%s" to "%s"', $old_value, self::VALUE_MAP[ $old_value ] ) );
			}
			return self::VALUE_MAP[ $old_value ];
		}

		// Unknown value, default to draft.
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( sprintf( 'Wisewand: Unknown publish mode "%s", defaulting to "draft"', $old_value ) );
		}
		return 'draft';
	}

	/**
	 * Get publish mode with fallback and translation
	 *
	 * Provides backward compatibility by translating old values to new values.
	 * Used throughout the codebase to safely get publish mode settings.
	 *
	 * @since 2.1.2
	 *
	 * @param string $option_name Option name to retrieve.
	 * @param string $default     Default value if option doesn't exist.
	 * @return string Translated publish mode value.
	 */
	public static function get_publish_mode_with_fallback( $option_name = 'wisewand_rss_publish_mode', $default = 'draft' ) {
		$options = new Options();
		$value   = $options->get( $option_name, $default );

		return self::translate_old_to_new( $value );
	}

	/**
	 * Maybe migrate global publish mode option
	 *
	 * Checks if the global publish mode option uses old values and migrates
	 * to new values if needed. Safe to call multiple times.
	 *
	 * @since 2.1.2
	 *
	 * @return bool True if migration occurred, false otherwise.
	 */
	public static function maybe_migrate_global_mode() {
		$options      = new Options();
		$current_mode = $options->get( 'wisewand_rss_publish_mode', 'draft' );

		// Already migrated or empty.
		if ( empty( $current_mode ) || in_array( $current_mode, array( 'immediate', 'scheduled', 'draft' ), true ) ) {
			return false;
		}

		// Needs migration.
		$new_mode = self::translate_old_to_new( $current_mode );

		if ( $new_mode !== $current_mode ) {
			$options->set( 'wisewand_rss_publish_mode', $new_mode );

			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( sprintf( 'Wisewand: Migrated global publish mode from "%s" to "%s"', $current_mode, $new_mode ) );
			}

			return true;
		}

		return false;
	}

	/**
	 * Get supported publish mode values
	 *
	 * Returns array of valid new publish mode values.
	 *
	 * @since 2.1.2
	 *
	 * @return array Valid publish mode values.
	 */
	public static function get_valid_modes() {
		return array( 'immediate', 'scheduled', 'draft' );
	}
}
