<?php
/**
 * Migration 018: Add Inheritance Flags to RSS Feeds
 *
 * Adds per-field inheritance flags to enable granular settings control:
 * - inherit_import_frequency: Inherit import_schedule_type from global settings
 * - inherit_author: Inherit target_author from global settings
 * - inherit_categories: Inherit target_categories from global settings
 * - inherit_publish_mode: Inherit publish_mode from global settings
 * - inherit_max_articles: Inherit max_articles_per_sync from global settings
 * - inherit_ai_categories: Inherit use_ai_category_matching from global settings
 *
 * These flags enable "Hybrid Configuration" where feeds can use
 * some global settings and override others.
 *
 * @package Wisewand
 * @since 2.1.5
 * @license GPL-2.0-or-later
 */

namespace Wisewand\Database\Migrations;

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

/**
 * Migration class for adding inheritance flags
 *
 * @since 2.1.5
 */
class Migration_018_Add_Inheritance_Flags {

	/**
	 * Run the migration
	 *
	 * @since 2.1.5
	 * @global wpdb $wpdb WordPress database abstraction object.
	 * @return bool True if migration ran successfully or already completed.
	 */
	public static function run() {
		global $wpdb;

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

		// Check if table exists (upgrade scenario only).
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) );

		if ( ! $table_exists ) {
			// Table doesn't exist yet - skip migration (fresh install).
			return true;
		}

		// Check if inherit_author column already exists.
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter
		$column_exists = $wpdb->get_results(
			$wpdb->prepare( "SHOW COLUMNS FROM `{$table_name}` LIKE %s", 'inherit_author' )
		);
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		if ( ! empty( $column_exists ) ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'Wisewand: Migration 18 already completed (inherit_author column exists)' );
			}
			return true;
		}

		// Migration needed.
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( 'Wisewand: Starting Migration 18 - Adding inheritance flags to RSS feeds' );
		}

		// Add inheritance flag columns.
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter
		$result = $wpdb->query(
			"ALTER TABLE `{$table_name}`
			ADD COLUMN inherit_import_frequency TINYINT(1) DEFAULT 0 COMMENT 'Inherit import_schedule_type from global settings' AFTER import_schedule_type,
		ADD COLUMN inherit_author TINYINT(1) DEFAULT 0 COMMENT 'Inherit target_author from global settings' AFTER target_author,
			ADD COLUMN inherit_categories TINYINT(1) DEFAULT 0 COMMENT 'Inherit target_categories from global settings' AFTER target_categories,
			ADD COLUMN inherit_publish_mode TINYINT(1) DEFAULT 0 COMMENT 'Inherit publish_mode from global settings' AFTER publish_mode,
			ADD COLUMN inherit_max_articles TINYINT(1) DEFAULT 0 COMMENT 'Inherit max_articles_per_sync from global settings' AFTER max_articles_per_sync,
			ADD COLUMN inherit_ai_categories TINYINT(1) DEFAULT 0 COMMENT 'Inherit use_ai_category_matching from global settings' AFTER use_ai_category_matching"
		);
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		if ( $wpdb->last_error ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'Wisewand: Migration 18 error: ' . $wpdb->last_error );
			}
			return false;
		}

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( 'Wisewand: Migration 18 completed successfully - Inheritance flags added' );
			error_log( 'Wisewand: Hybrid configuration now supported (selective inheritance per field)' );
		}

		return $result !== false;
	}

	/**
	 * Rollback the migration
	 *
	 * @since 2.1.5
	 * @global wpdb $wpdb WordPress database abstraction object.
	 * @return bool True if rollback succeeded.
	 */
	public static function rollback() {
		global $wpdb;

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

		// Check if table exists.
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) );

		if ( ! $table_exists ) {
			return true;
		}

		// Drop inheritance columns if they exist.
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter
		$wpdb->query(
			"ALTER TABLE `{$table_name}`
			DROP COLUMN IF EXISTS inherit_import_frequency,
		DROP COLUMN IF EXISTS inherit_author,
			DROP COLUMN IF EXISTS inherit_categories,
			DROP COLUMN IF EXISTS inherit_publish_mode,
			DROP COLUMN IF EXISTS inherit_max_articles,
			DROP COLUMN IF EXISTS inherit_ai_categories"
		);
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			if ( $wpdb->last_error ) {
				error_log( 'Wisewand: Migration 18 rollback error: ' . $wpdb->last_error );
				return false;
			}
			error_log( 'Wisewand: Migration 18 rolled back successfully' );
		}

		return true;
	}
}
