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

namespace SEOvaultSEO\Frontend;

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

/**
 * Handles SEO title output
 */
class Title {

	/**
	 * Initialize title filter
	 */
	public static function init() {
		if ( ! \SEOvaultSEO\Compatibility\Detector::should_output_seo() ) {
			return;
		}

		add_filter( 'pre_get_document_title', array( __CLASS__, 'filter_document_title' ), 15 );
	}

	/**
	 * Filter WordPress document title
	 *
	 * @param string $title Current title
	 * @return string
	 */
	public static function filter_document_title( $title ) {
		if ( ! \SEOvaultSEO\Compatibility\Detector::should_output_seo() ) {
			return $title;
		}

		$seovault_title = self::get_title();

		if ( ! empty( $seovault_title ) ) {
			return $seovault_title;
		}

		return $title;
	}

	/**
	 * Get SEO title for current context
	 *
	 * @return string
	 */
	public static function get_title() {
		return RequestCache::remember(
			'title',
			function () {
				// Check if this is the homepage
				if ( is_front_page() && is_home() ) {
					return self::get_home_title();
				}

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

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

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

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

				// 404 page
				if ( is_404() ) {
					return self::get_404_title();
				}

				// Default to empty so the filter can fall back to the original WP title.
				// wp_get_document_title() would re-trigger pre_get_document_title and cause recursion.
				return '';
			}
		);
	}

	/**
	 * Get homepage title
	 *
	 * @return string
	 */
	private static function get_home_title() {
		$custom_title = \SEOvaultSEO\Core\Options::get_home_title();

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

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

		return get_bloginfo( 'name' );
	}

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

		if ( $page_id ) {
			$custom_title = Formatter::get_effective_title( $page_id );
			if ( ! empty( $custom_title ) ) {
				return $custom_title;
			}
		}

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

		return get_bloginfo( 'name' );
	}

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

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

		return Formatter::get_effective_title( $post_id );
	}

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

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

		$sep = self::get_title_separator_glue();

		if ( is_category() ) {
			$title = single_cat_title( '', false ) . $sep . get_bloginfo( 'name' );
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $title );
		}

		if ( is_tag() ) {
			$title = single_tag_title( '', false ) . $sep . get_bloginfo( 'name' );
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $title );
		}

		if ( is_tax() ) {
			$term = get_queried_object();
			if ( $term && ! is_wp_error( $term ) && ! empty( $term->name ) ) {
				$title = $term->name . $sep . get_bloginfo( 'name' );
				return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $title );
			}
		}

		if ( is_author() ) {
			$author = get_queried_object();
			$title  = get_the_author_meta( 'display_name', $author->ID ) . $sep . get_bloginfo( 'name' );
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $title );
		}

		if ( is_date() ) {
			$title = get_the_date() . $sep . get_bloginfo( 'name' );
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $title );
		}

		if ( is_post_type_archive() ) {
			$title = post_type_archive_title( '', false ) . $sep . get_bloginfo( 'name' );
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $title );
		}

		return get_bloginfo( 'name' );
	}

	/**
	 * Get 404 page title
	 *
	 * @return string
	 */
	private static function get_404_title() {
		return __( 'Page Not Found', 'seovault-ai' ) . self::get_title_separator_glue() . get_bloginfo( 'name' );
	}

	/**
	 * Build spaced title-separator glue from settings (fallback handled by Options).
	 *
	 * @return string
	 */
	private static function get_title_separator_glue() {
		return ' ' . \SEOvaultSEO\Core\Options::get_title_separator() . ' ';
	}
}
