<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @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.
 */

/**
 * Migration 13: Retroactive External ID Fix
 *
 * Syncs _wisewand_external_id meta from calendar and RSS tables to WordPress posts.
 * Fixes the issue where already published posts show as "Import Now" in the list.
 *
 * @package Wisewand
 * @since 1.6.35
 */

namespace Wisewand\Database\Migrations;

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

/**
 * Migration 13 class
 */
class Migration_013_Retroactive_External_Id_Fix {

	/**
	 * Run the migration
	 *
	 * @return bool True on success
	 */
	public static function run() {
		global $wpdb;

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( 'Wisewand: Starting Migration 13 - Retroactive External ID Fix' );
		}

		$calendar_table = $wpdb->prefix . 'wisewand_editorial_calendar';
		$rss_table      = $wpdb->prefix . 'wisewand_rss_articles';
		$fixed_count    = 0;

		$wpdb->query( 'START TRANSACTION' );

		// 1. Fix from Editorial Calendar
		// Get all entries with both IDs
		$calendar_entries = $wpdb->get_results(
			"SELECT wisewand_article_id, wordpress_post_id 
			 FROM {$calendar_table} 
			 WHERE wordpress_post_id IS NOT NULL 
			 AND wordpress_post_id > 0 
			 AND wisewand_article_id IS NOT NULL 
			 AND wisewand_article_id != ''"
		);

		if ( $calendar_entries ) {
			foreach ( $calendar_entries as $entry ) {
				$post_id     = $entry->wordpress_post_id;
				$external_id = $entry->wisewand_article_id;

				// Check if post exists
				$post = get_post( $post_id );
				if ( ! $post ) {
					continue;
				}

				// Check if meta already exists and is correct
				$current_meta = get_post_meta( $post_id, '_wisewand_external_id', true );

				if ( $current_meta !== $external_id ) {
					update_post_meta( $post_id, '_wisewand_external_id', $external_id );
					++$fixed_count;
				}
			}
		}

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( "Wisewand: Migration 13 - Fixed {$fixed_count} posts from Calendar" );
		}

		// 2. Fix from RSS Articles
		// Get all entries with both IDs
		$rss_entries = $wpdb->get_results(
			"SELECT wisewand_article_id, wordpress_post_id 
			 FROM {$rss_table} 
			 WHERE wordpress_post_id IS NOT NULL 
			 AND wordpress_post_id > 0 
			 AND wisewand_article_id IS NOT NULL 
			 AND wisewand_article_id != ''"
		);

		$rss_fixed_count = 0;
		if ( $rss_entries ) {
			foreach ( $rss_entries as $entry ) {
				$post_id     = $entry->wordpress_post_id;
				$external_id = $entry->wisewand_article_id;

				// Check if post exists
				$post = get_post( $post_id );
				if ( ! $post ) {
					continue;
				}

				// Check if meta already exists and is correct
				$current_meta = get_post_meta( $post_id, '_wisewand_external_id', true );

				if ( $current_meta !== $external_id ) {
					update_post_meta( $post_id, '_wisewand_external_id', $external_id );
					++$rss_fixed_count;
				}
			}
		}

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( "Wisewand: Migration 13 - Fixed {$rss_fixed_count} posts from RSS" );
		}

		$wpdb->query( 'COMMIT' );

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( 'Wisewand: Migration 13 completed successfully' );
		}

		update_option( 'wisewand_migration_13_completed', current_time( 'mysql' ) );

		return true;
	}
}
