<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @since   1.4.0
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

namespace Wisewand\Database\Migrations;

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

// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- error_log() only used within WP_DEBUG conditionals; direct database calls and schema changes required for migrations

/**
 * Migration 011: Add publish mode settings to RSS feeds
 *
 * Adds publish_mode column to wisewand_rss_feeds table to support
 * three publication modes: schedule (current behavior), publish (immediate),
 * and draft (needs review).
 *
 * @since 1.4.0
 */
class Migration_011_Add_Publish_Mode {

	/**
	 * Run the migration
	 *
	 * Adds publish_mode column with safe default value 'global'.
	 * Migration is idempotent - safe to run multiple times.
	 *
	 * @since 1.4.0
	 * @global wpdb $wpdb WordPress database abstraction object.
	 * @return bool True if migration successful or already completed.
	 */
	public static function run() {
		global $wpdb;

		$table_name = $wpdb->prefix . 'wisewand_rss_feeds';

		// Check if column already exists (migration already completed).
		$column_exists = $wpdb->get_results(
			$wpdb->prepare(
				// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from $wpdb->prefix is safe
				"SHOW COLUMNS FROM `{$table_name}` LIKE %s",
				'publish_mode'
			)
		);

		if ( ! empty( $column_exists ) ) {
			if ( WP_DEBUG ) {
				error_log( 'Wisewand: Migration 11 already completed (publish_mode column exists)' );
			}
			return true;
		}

		// Migration needed.
		if ( WP_DEBUG ) {
			error_log( 'Wisewand: Starting Migration 11 - Adding publish_mode column for RSS feeds' );
		}

		// Add publish_mode column after target_post_status.
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from $wpdb->prefix is safe
		$result = $wpdb->query(
			"ALTER TABLE `{$table_name}`
			ADD COLUMN publish_mode varchar(20) DEFAULT 'global'
			COMMENT 'Publish mode: global, schedule, publish, draft'
			AFTER target_post_status"
		);

		if ( $result === false ) {
			if ( WP_DEBUG ) {
				error_log( 'Wisewand: Migration 11 FAILED: ' . $wpdb->last_error );
			}
			return false;
		}

		if ( WP_DEBUG ) {
			error_log( 'Wisewand: Migration 11 SUCCESS - publish_mode column added' );
			error_log( 'Wisewand: RSS feeds now support three publish modes: schedule (default), publish (immediate), draft (review)' );
			update_option( 'wisewand_migration_11_completed', current_time( 'mysql' ) );
		}

		return true;
	}

	/**
	 * Rollback the migration
	 *
	 * Removes publish_mode column from RSS feeds table.
	 * Used for testing or emergency rollback.
	 *
	 * @since 1.4.0
	 * @global wpdb $wpdb WordPress database abstraction object.
	 * @return bool True if rollback successful.
	 */
	public static function down() {
		global $wpdb;

		$table_name = $wpdb->prefix . 'wisewand_rss_feeds';

		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from $wpdb->prefix is safe
		$result = $wpdb->query(
			"ALTER TABLE `{$table_name}` DROP COLUMN IF EXISTS publish_mode"
		);

		if ( WP_DEBUG ) {
			if ( $result !== false ) {
				error_log( 'Wisewand: Migration 11 ROLLBACK - publish_mode column removed' );
			} else {
				error_log( 'Wisewand: Migration 11 ROLLBACK FAILED: ' . $wpdb->last_error );
			}
		}

		return $result !== false;
	}
}
