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

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

// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log -- error_log() is only used within WP_DEBUG conditionals for debugging

class Content_Cleaner {

	/**
	 * Clean and process Wisewand content for WordPress
	 */
	public static function clean_content( $content, $options = array() ) {
		$default_options = array(
			'remove_h1'              => true,
			'fix_headings'           => true,
			'clean_empty_paragraphs' => true,
			'optimize_images'        => true,
			'preserve_audio_players' => true,
			'fix_encoded_html'       => true,
		);

		$options = array_merge( $default_options, $options );

		// Log original content for debugging
		if ( WP_DEBUG ) {
			error_log( 'Wisewand Content Cleaning - Original length: ' . strlen( $content ) );
		}
		if ( WP_DEBUG ) {
			error_log( 'Wisewand Content Cleaning - Original H1 count: ' . substr_count( strtolower( $content ), '<h1' ) );
		}

		// Fix encoded HTML tags FIRST (convert &lt;img&gt; back to <img>)
		if ( $options['fix_encoded_html'] ) {
			$content = self::fix_encoded_html_tags( $content );
		}

		// Remove H1 tags COMPLETELY (tags + content)
		if ( $options['remove_h1'] ) {
			$content = self::remove_h1_tags( $content );
		}

		// Fix heading hierarchy (convert remaining headings)
		if ( $options['fix_headings'] ) {
			$content = self::fix_heading_hierarchy( $content );
		}

		// Clean empty paragraphs
		if ( $options['clean_empty_paragraphs'] ) {
			$content = self::clean_empty_paragraphs( $content );
		}

		// Optimize image attributes
		if ( $options['optimize_images'] ) {
			$content = self::optimize_image_attributes( $content );
		}

		// Preserve existing audio players
		if ( $options['preserve_audio_players'] ) {
			$content = self::preserve_audio_players( $content );
		}

		// Log final content for debugging
		if ( WP_DEBUG ) {
			error_log( 'Wisewand Content Cleaning - Final length: ' . strlen( $content ) );
		}
		if ( WP_DEBUG ) {
			error_log( 'Wisewand Content Cleaning - Final H1 count: ' . substr_count( strtolower( $content ), '<h1' ) );
		}

		return $content;
	}

	/**
	 * Fix encoded HTML tags (convert &lt;img&gt; back to <img>)
	 */
	public static function fix_encoded_html_tags( $content ) {
		// Fix encoded img tags
		$content = preg_replace( '/&lt;img([^&]*?)\/&gt;/i', '<img$1/>', $content );
		$content = preg_replace( '/&lt;img([^&]*?)&gt;/i', '<img$1>', $content );

		// Fix other common encoded tags
		$content = str_replace( array( '&lt;', '&gt;' ), array( '<', '>' ), $content );

		// Fix quotes in attributes
		$content = preg_replace( '/(\w+)=(["\'])([^"\']*?)\2/', '$1="$3"', $content );

		return $content;
	}

	/**
	 * Remove H1 tags AND their content completely (since WordPress post title handles this)
	 */
	public static function remove_h1_tags( $content ) {
		// Log original content for debugging
		$original_h1_count = substr_count( strtolower( $content ), '<h1' );

		// Remove H1 tags and ALL content inside them (including the text)
		// This regex handles multiline H1 content and various attributes
		$content = preg_replace( '/<h1[^>]*>.*?<\/h1>/is', '', $content );

		// Also handle self-closing or malformed H1 tags
		$content = preg_replace( '/<h1[^>]*\/?>/i', '', $content );

		// Remove any orphaned closing h1 tags
		$content = preg_replace( '/<\/h1>/i', '', $content );

		// Clean up any multiple line breaks left behind
		$content = preg_replace( '/\n\s*\n\s*\n+/', "\n\n", $content );

		// Remove empty paragraphs that might be left after H1 removal
		$content = preg_replace( '/<p[^>]*>[\s\n\r]*<\/p>/i', '', $content );

		// Clean up any leading/trailing whitespace
		$content = trim( $content );

		// Log the cleaning results for debugging
		$new_h1_count = substr_count( strtolower( $content ), '<h1' );
		if ( WP_DEBUG ) {
			error_log( "Wisewand H1 Removal: {$original_h1_count} H1 tags found, {$new_h1_count} remaining after cleaning" );
		}

		return $content;
	}

	/**
	 * Fix heading hierarchy by shifting all headings down one level
	 */
	public static function fix_heading_hierarchy( $content ) {
		// Convert H6 to H6 (keep as is - deepest level)
		// Convert H5 to H6
		$content = preg_replace( '/<h5([^>]*)>/i', '<h6$1>', $content );
		$content = preg_replace( '/<\/h5>/i', '</h6>', $content );

		// Convert H4 to H5
		$content = preg_replace( '/<h4([^>]*)>/i', '<h5$1>', $content );
		$content = preg_replace( '/<\/h4>/i', '</h5>', $content );

		// Convert H3 to H4
		$content = preg_replace( '/<h3([^>]*)>/i', '<h4$1>', $content );
		$content = preg_replace( '/<\/h3>/i', '</h4>', $content );

		// Convert H2 to H3
		$content = preg_replace( '/<h2([^>]*)>/i', '<h3$1>', $content );
		$content = preg_replace( '/<\/h2>/i', '</h3>', $content );

		// Convert any remaining H1 to H2
		$content = preg_replace( '/<h1([^>]*)>/i', '<h2$1>', $content );
		$content = preg_replace( '/<\/h1>/i', '</h2>', $content );

		return $content;
	}

	/**
	 * Remove empty paragraphs and clean up spacing
	 */
	public static function clean_empty_paragraphs( $content ) {
		// Remove empty paragraphs
		$content = preg_replace( '/<p[^>]*>[\s]*<\/p>/i', '', $content );

		// Remove paragraphs with only whitespace or &nbsp;
		$content = preg_replace( '/<p[^>]*>[\s&nbsp;]*<\/p>/i', '', $content );

		// Clean up multiple consecutive line breaks
		$content = preg_replace( '/(<\/p>\s*){2,}/', '</p>' . "\n", $content );

		return $content;
	}

	/**
	 * Optimize image attributes for better performance and accessibility
	 */
	public static function optimize_image_attributes( $content ) {
		return preg_replace_callback(
			'/<img([^>]+)>/i',
			function ( $matches ) {
				$img_tag    = $matches[0];
				$attributes = $matches[1];

				// Add loading="lazy" if not present
				if ( strpos( $attributes, 'loading=' ) === false ) {
					$attributes .= ' loading="lazy"';
				}

				// Add decoding="async" if not present
				if ( strpos( $attributes, 'decoding=' ) === false ) {
					$attributes .= ' decoding="async"';
				}

				// Ensure alt attribute exists (for accessibility)
				if ( strpos( $attributes, 'alt=' ) === false ) {
					$attributes .= ' alt=""';
				}

				return '<img' . $attributes . '>';
			},
			$content
		);
	}

	/**
	 * Preserve existing audio players (don't duplicate)
	 */
	public static function preserve_audio_players( $content ) {
		// Mark existing audio players to avoid duplication
		return preg_replace( '/<audio([^>]*)>/i', '<audio$1 data-wisewand-preserved="true">', $content );
	}

	/**
	 * Generate simple HTML audio player that works universally
	 */
	public static function generate_audio_player( $audio_url, $title = null ) {
		// Simple, universal audio player HTML that works on any website
		$audio_html  = '<div class="wp-audio-player" style="margin: 20px 0;">';
		$audio_html .= '<audio style="width: 100%;" controls="controls">';
		$audio_html .= '<source src="' . esc_url( $audio_url ) . '" type="audio/mpeg">';
		$audio_html .= '<source src="' . esc_url( $audio_url ) . '" type="audio/ogg">';
		$audio_html .= '<source src="' . esc_url( $audio_url ) . '" type="audio/wav">';
		$audio_html .= 'Your browser does not support the audio element.';
		$audio_html .= '</audio>';
		$audio_html .= '</div>';

		return $audio_html;
	}

	/**
	 * Extract title from H1 for use as post title
	 */
	public static function extract_h1_title( $content ) {
		preg_match( '/<h1[^>]*>(.*?)<\/h1>/is', $content, $matches );
		if ( ! empty( $matches[1] ) ) {
			return wp_strip_all_tags( $matches[1] );
		}
		return '';
	}

	/**
	 * Remove specific HTML elements while preserving content
	 */
	public static function remove_html_element( $content, $element ) {
		$pattern = "/<{$element}[^>]*>(.*?)<\/{$element}>/is";
		return preg_replace( $pattern, '$1', $content );
	}

	/**
	 * Insert content at the beginning of post content
	 */
	public static function prepend_to_content( $existing_content, $new_content ) {
		// Add proper spacing between new content and existing content
		return $new_content . "\n\n" . $existing_content;
	}

	/**
	 * Clean and format content for specific post types
	 */
	public static function format_for_post_type( $content, $post_type ) {
		switch ( $post_type ) {
			case 'page':
				// For pages, keep more formal structure
				$content = self::clean_content(
					$content,
					array(
						'remove_h1'    => true,
						'fix_headings' => false, // Keep original heading structure for pages
					)
				);
				break;

			case 'post':
			default:
				// For posts, optimize for blog reading
				$content = self::clean_content(
					$content,
					array(
						'remove_h1'    => true,
						'fix_headings' => false, // Keep original structure (Wisewand API provides correct H2/H3)
					)
				);
				break;
		}

		return $content;
	}
}
