<?php
/**
 * Migration 016: Add RSS schedule fields
 *
 * Adds schedule_mode and schedule_time columns to rss_feeds table
 * for global and per-feed scheduling control.
 *
 * @package Wisewand
 * @since 2.1.3
 * @license GPL-2.0-or-later
 */

namespace Wisewand\Database\Migrations;

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

/**
 * Migration 016: Add RSS scheduling fields
 *
 * @since 2.1.3
 */
class Migration_016_Add_RSS_Schedule_Fields {

	/**
	 * Run migration - add schedule columns
	 *
	 * @since 2.1.3
	 * @global wpdb $wpdb WordPress database instance.
	 * @return bool True if successful.
	 */
	public static function run() {
		global $wpdb;

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

		// START TRANSACTION for atomicity
		$wpdb->query( 'START TRANSACTION' );

		try {
			// Check if schedule_mode column exists
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$column_exists = $wpdb->get_var(
				$wpdb->prepare(
					"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
					WHERE TABLE_SCHEMA = %s
					AND TABLE_NAME = %s
					AND COLUMN_NAME = %s",
					DB_NAME,
					$table_name,
					'schedule_mode'
				)
			);

			if ( $column_exists ) {
				// COMMIT and return (already migrated)
				$wpdb->query( 'COMMIT' );
				return true;
			}

			// Add schedule_mode column
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
			$result1 = $wpdb->query(
				$wpdb->prepare(
					"ALTER TABLE %i ADD COLUMN schedule_mode VARCHAR(20) NOT NULL DEFAULT 'inherit'
					COMMENT 'Publish schedule mode: inherit, custom'
					AFTER auto_import_enabled",
					$table_name
				)
			);

			if ( false === $result1 ) {
				throw new \Exception( 'Failed to add schedule_mode column: ' . $wpdb->last_error );
			}

			// Add schedule_time column
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
			$result2 = $wpdb->query(
				$wpdb->prepare(
					"ALTER TABLE %i ADD COLUMN schedule_time TIME NULL DEFAULT NULL
					COMMENT 'Custom publish time (HH:MM:SS format, WordPress timezone)'
					AFTER schedule_mode",
					$table_name
				)
			);

			if ( false === $result2 ) {
				throw new \Exception( 'Failed to add schedule_time column: ' . $wpdb->last_error );
			}

			// Set default global options if not exist
			if ( false === get_option( 'wisewand_rss_publish_mode' ) ) {
				add_option( 'wisewand_rss_publish_mode', 'immediate', '', 'no' );
			}

			if ( false === get_option( 'wisewand_rss_publish_time' ) ) {
				add_option( 'wisewand_rss_publish_time', '11:00', '', 'no' );
			}

			// COMMIT transaction
			$wpdb->query( 'COMMIT' );

			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'Wisewand Migration 016: Successfully added RSS schedule fields' );
			}

			return true;

		} catch ( \Exception $e ) {
			// ROLLBACK on any error
			$wpdb->query( 'ROLLBACK' );

			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'Wisewand Migration 016 FAILED (rolled back): ' . $e->getMessage() );
			}

			return false;
		}
	}
}
