<?php
/**
 * Migration 017: Consolidate Publish Mode Options
 *
 * Consolidates duplicate publish mode settings into a single unified option.
 * Migrates values from old options to new unified option.
 *
 * @package Wisewand
 * @since 2.1.4
 * @license GPL-2.0-or-later
 */

namespace Wisewand\Database\Migrations;

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

/**
 * Class Migration_017_Consolidate_Publish_Mode_Options
 *
 * Consolidates the following old options into wisewand_rss_publish_mode:
 * - wisewand_rss_default_publish_mode
 * - global_rss_publish_mode (if exists)
 *
 * @package Wisewand\Database\Migrations
 * @since 2.1.4
 */
class Migration_017_Consolidate_Publish_Mode_Options {

	/**
	 * Migration version
	 *
	 * @var string
	 */
	const VERSION = '017';

	/**
	 * Migration description
	 *
	 * @var string
	 */
	const DESCRIPTION = 'Consolidate publish mode options into unified setting';

	/**
	 * Run the migration
	 *
	 * Consolidates duplicate publish mode settings.
	 *
	 * @since 2.1.4
	 * @return bool|\WP_Error True on success, WP_Error on failure.
	 */
	public function up() {
		// Get current unified option value (may already be set)
		$current_unified = get_option( 'wisewand_rss_publish_mode', false );

		// If unified option already exists, no migration needed
		if ( false !== $current_unified ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'Wisewand Migration 017: Unified option already exists, skipping migration' );
			}
			return true;
		}

		// Priority order for migration:
		// 1. wisewand_rss_default_publish_mode (most likely to be configured)
		// 2. global_rss_publish_mode (old standalone section)
		// 3. Default to 'draft'

		$old_default_mode = get_option( 'wisewand_rss_default_publish_mode', false );
		$old_global_mode  = get_option( 'global_rss_publish_mode', false );

		$value_to_migrate = false;

		if ( false !== $old_default_mode ) {
			$value_to_migrate = $old_default_mode;
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( "Wisewand Migration 017: Migrating from wisewand_rss_default_publish_mode: {$value_to_migrate}" );
			}
		} elseif ( false !== $old_global_mode ) {
			$value_to_migrate = $old_global_mode;
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( "Wisewand Migration 017: Migrating from global_rss_publish_mode: {$value_to_migrate}" );
			}
		}

		// Map old values to new format if needed
		$unified_value = 'draft'; // Safe default

		if ( false !== $value_to_migrate ) {
			// Normalize the value
			$normalized = strtolower( trim( $value_to_migrate ) );

			// Map old formats to new unified format
			switch ( $normalized ) {
				case 'publish':
				case 'immediate':
					$unified_value = 'immediate';
					break;

				case 'draft':
					$unified_value = 'draft';
					break;

				case 'schedule':
				case 'scheduled':
					$unified_value = 'scheduled';
					break;

				default:
					// Unknown value, log warning and use safe default
					if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
						error_log( "Wisewand Migration 017: Unknown publish mode '{$normalized}', defaulting to 'draft'" );
					}
					$unified_value = 'draft';
			}
		}

		// Save the unified option
		$result = update_option( 'wisewand_rss_publish_mode', $unified_value );

		if ( ! $result && get_option( 'wisewand_rss_publish_mode' ) !== $unified_value ) {
			return new \WP_Error(
				'migration_failed',
				__( 'Failed to save unified publish mode option', 'wisewand-ai-content-writer-seo-content-generator' )
			);
		}

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( "Wisewand Migration 017: Successfully set wisewand_rss_publish_mode to '{$unified_value}'" );
		}

		// Note: We DO NOT delete old options to maintain backward compatibility
		// for rollback scenarios. They will be ignored by new code.

		return true;
	}

	/**
	 * Rollback the migration
	 *
	 * Removes the unified option. Old options remain for fallback.
	 *
	 * @since 2.1.4
	 * @return bool|\WP_Error True on success, WP_Error on failure.
	 */
	public function down() {
		// Simply remove the unified option
		// Old options remain for fallback
		$result = delete_option( 'wisewand_rss_publish_mode' );

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			if ( $result ) {
				error_log( 'Wisewand Migration 017: Rollback complete - unified option removed' );
			} else {
				error_log( 'Wisewand Migration 017: Rollback - option did not exist or could not be deleted' );
			}
		}

		return true;
	}

	/**
	 * Get migration version
	 *
	 * @since 2.1.4
	 * @return string Version number.
	 */
	public function get_version() {
		return self::VERSION;
	}

	/**
	 * Get migration description
	 *
	 * @since 2.1.4
	 * @return string Description.
	 */
	public function get_description() {
		return self::DESCRIPTION;
	}
}
