<?php
/**
 * Frontend Description Output
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Frontend;

use SEOvaultSEO\Utils\Formatter;
use SEOvaultSEO\Utils\RequestCache;

/**
 * Handles meta description output
 */
class Description {

	/**
	 * Output meta description
	 */
	public static function output() {
		$description = self::get_description_filtered();

		if ( empty( $description ) ) {
			return;
		}

		echo '<meta name="description" content="' . esc_attr( $description ) . '" />' . "\n";
	}

	/**
	 * Get meta description for current context
	 *
	 * @return string
	 */
	public static function get_description() {
		return RequestCache::remember(
			self::get_description_cache_key(),
			function () {
				// Check if this is the homepage
				if ( is_front_page() && is_home() ) {
					return self::get_home_description();
				}

				if ( is_front_page() ) {
					return self::get_home_description();
				}

				if ( is_home() ) {
					return self::get_blog_description();
				}

				// Singular posts/pages
				if ( is_singular() ) {
					return self::get_singular_description();
				}

				// Archives
				if ( is_archive() ) {
					return self::get_archive_description();
				}

				// In manual mode, do not auto-generate — return empty.
				if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
					return '';
				}

				// Default to blog description
				return get_bloginfo( 'description' );
			}
		);
	}

	/**
	 * Build a cache key that reflects the current query context.
	 *
	 * @return string
	 */
	private static function get_description_cache_key() {
		$object_id = (int) get_queried_object_id();

		return implode(
			':',
			array(
				'description',
				is_front_page() ? 'fp1' : 'fp0',
				is_home() ? 'home1' : 'home0',
				is_singular() ? 'singular' . $object_id : 'singular0',
				is_archive() ? 'archive' . $object_id : 'archive0',
				is_search() ? 'search' : 'search0',
				is_404() ? '404' : '4040',
			)
		);
	}

	/**
	 * Get meta description with filter.
	 *
	 * @return string
	 */
	public static function get_description_filtered() {
		$description = self::get_description();
		return apply_filters( 'seovaultseo_description', $description, get_queried_object_id() );
	}

	/**
	 * Get homepage description
	 *
	 * @return string
	 */
	private static function get_home_description() {
		$custom_description = \SEOvaultSEO\Core\Options::get_home_description();

		if ( ! empty( $custom_description ) ) {
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $custom_description );
		}

		// In manual mode, do not auto-generate — return empty.
		if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
			return '';
		}

		return get_bloginfo( 'description' );
	}

	/**
	 * Get blog/posts page description
	 *
	 * @return string
	 */
	private static function get_blog_description() {
		$page_id = get_option( 'page_for_posts' );

		if ( $page_id ) {
			$custom_description = Formatter::get_effective_description( $page_id );
			if ( ! empty( $custom_description ) ) {
				return $custom_description;
			}
		}

		// In manual mode, do not auto-generate — return empty.
		if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
			return '';
		}

		$generated = Formatter::generate_blog_description();
		if ( ! empty( $generated ) ) {
			return $generated;
		}

		return get_bloginfo( 'description' );
	}

	/**
	 * Get singular post/page description
	 *
	 * @return string
	 */
	private static function get_singular_description() {
		$post_id = get_queried_object_id();

		if ( ! $post_id ) {
			return '';
		}

		return Formatter::get_effective_description( $post_id );
	}

	/**
	 * Get archive description
	 *
	 * @return string
	 */
	private static function get_archive_description() {
		if ( is_category() || is_tag() || is_tax() ) {
			$term_id = get_queried_object_id();
			if ( $term_id ) {
				$custom_description = \SEOvaultSEO\Core\TermMeta::get_seo_description( $term_id );
				if ( '' !== $custom_description ) {
					return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $custom_description );
				}
			}
		}

		// In manual mode, do not auto-generate — return empty.
		if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
			return '';
		}

		if ( is_category() ) {
			$term = get_queried_object();
			if ( $term && ! empty( $term->description ) ) {
				return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( wp_strip_all_tags( $term->description ) );
			}
		}

		if ( is_tag() ) {
			$term = get_queried_object();
			if ( $term && ! empty( $term->description ) ) {
				return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( wp_strip_all_tags( $term->description ) );
			}
		}

		if ( is_tax() ) {
			$term = get_queried_object();
			if ( $term && ! is_wp_error( $term ) && ! empty( $term->description ) ) {
				return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( wp_strip_all_tags( $term->description ) );
			}
		}

		if ( is_author() ) {
			$author_bio = get_the_author_meta( 'description' );
			if ( ! empty( $author_bio ) ) {
				return Formatter::truncate( Formatter::clean_text( $author_bio ), 160 );
			}
		}

		if ( is_post_type_archive() ) {
			$post_type = get_queried_object();
			if ( $post_type && ! empty( $post_type->description ) ) {
				return Formatter::truncate( Formatter::clean_text( $post_type->description ), 160 );
			}
		}

		return get_bloginfo( 'description' );
	}
}
