<?php
/**
 * Frontend Open Graph Output
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Frontend;

use SEOvaultSEO\Utils\Formatter;
use SEOvaultSEO\Core\Options;

/**
 * Handles Open Graph tags output
 */
class OpenGraph {

	/**
	 * Output Open Graph tags
	 */
	public static function output() {
		if ( ! Options::is_og_enabled() ) {
			return;
		}

		// Singular and homepage only.
		if ( ! is_singular() && ! is_front_page() ) {
			return;
		}

		$og_type        = self::get_og_type();
		$og_title       = self::get_og_title();
		$og_description = self::get_og_description();
		$og_url         = self::get_og_url();
		$og_image       = self::get_og_image();
		$site_name      = get_bloginfo( 'name' );

		if ( empty( $og_title ) && empty( $og_description ) && empty( $og_image ) ) {
			return;
		}

		echo '<!-- SEOvault AI Open Graph -->' . "\n";

		if ( ! empty( $og_type ) ) {
			echo '<meta property="og:type" content="' . esc_attr( $og_type ) . '" />' . "\n";
		}

		if ( ! empty( $og_title ) ) {
			echo '<meta property="og:title" content="' . esc_attr( $og_title ) . '" />' . "\n";
		}

		if ( ! empty( $og_description ) ) {
			echo '<meta property="og:description" content="' . esc_attr( $og_description ) . '" />' . "\n";
		}

		if ( ! empty( $og_url ) ) {
			echo '<meta property="og:url" content="' . esc_url( $og_url ) . '" />' . "\n";
		}

		if ( ! empty( $og_image ) ) {
			echo '<meta property="og:image" content="' . esc_url( $og_image ) . '" />' . "\n";
		}

		if ( ! empty( $site_name ) ) {
			echo '<meta property="og:site_name" content="' . esc_attr( $site_name ) . '" />' . "\n";
		}

		// Output article:publisher when Facebook page URL is set and OG type is article.
		if ( 'article' === $og_type ) {
			$facebook_page = Options::get_facebook_page_url();
			if ( ! empty( $facebook_page ) ) {
				echo '<meta property="article:publisher" content="' . esc_url( $facebook_page ) . '" />' . "\n";
			}
		}

		echo '<meta property="og:locale" content="' . esc_attr( get_locale() ) . '" />' . "\n";
	}

	/**
	 * Get OG type
	 *
	 * @return string
	 */
	private static function get_og_type() {
		if ( is_front_page() ) {
			return 'website';
		}

		if ( is_singular() ) {
			$post_type = get_post_type();

			if ( 'page' === $post_type ) {
				return 'website';
			}

			if ( 'post' === $post_type ) {
				return 'article';
			}
		}

		return apply_filters( 'seovaultseo_og_type', 'website' );
	}

	/**
	 * Get OG title
	 *
	 * @return string
	 */
	private static function get_og_title() {
		if ( is_front_page() ) {
			$home_title = \SEOvaultSEO\Core\Options::get_home_title();
			if ( ! empty( $home_title ) ) {
				return $home_title;
			}
			// In manual mode, do not auto-generate.
			if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
				return '';
			}
			return get_bloginfo( 'name' );
		}

		if ( is_singular() ) {
			$post_id = get_queried_object_id();
			if ( $post_id ) {
				return Formatter::get_effective_social_title( $post_id );
			}
		}

		if ( is_category() || is_tag() || is_tax() ) {
			return self::get_term_og_title();
		}

		return '';
	}

	/**
	 * Get OG description
	 *
	 * @return string
	 */
	private static function get_og_description() {
		if ( is_front_page() ) {
			$home_description = \SEOvaultSEO\Core\Options::get_home_description();
			if ( ! empty( $home_description ) ) {
				return $home_description;
			}
			// In manual mode, do not auto-generate.
			if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
				return '';
			}
			return get_bloginfo( 'description' );
		}

		if ( is_singular() ) {
			$post_id = get_queried_object_id();
			if ( $post_id ) {
				return Formatter::get_effective_og_description( $post_id );
			}
		}

		if ( is_category() || is_tag() || is_tax() ) {
			return self::get_term_og_description();
		}

		return '';
	}

	/**
	 * Get OG URL
	 *
	 * @return string
	 */
	private static function get_og_url() {
		if ( is_singular() ) {
			return get_permalink();
		}

		if ( is_front_page() ) {
			return home_url( '/' );
		}

		if ( is_category() || is_tag() || is_tax() ) {
			return Canonical::get_canonical();
		}

		return '';
	}

	/**
	 * Get OG image
	 *
	 * @return string
	 */
	private static function get_og_image() {
		if ( is_singular() ) {
			$post_id = get_queried_object_id();
			if ( $post_id ) {
				return Formatter::get_effective_og_image( $post_id );
			}
		}

		if ( is_front_page() ) {
			return Formatter::get_effective_og_image( 0 );
		}

		if ( is_category() || is_tag() || is_tax() ) {
			$term_id = get_queried_object_id();
			if ( $term_id ) {
				$og_image = \SEOvaultSEO\Core\TermMeta::get_og_image( $term_id );
				if ( '' !== $og_image ) {
					return $og_image;
				}
			}
			return Formatter::get_effective_og_image( 0 );
		}

		return '';
	}

	/**
	 * Term archive OG title (custom OG → SEO title → term name).
	 *
	 * @return string
	 */
	private static function get_term_og_title() {
		$term_id = get_queried_object_id();
		if ( ! $term_id ) {
			return '';
		}

		$og_title = \SEOvaultSEO\Core\TermMeta::get_og_title( $term_id );
		if ( '' !== $og_title ) {
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $og_title );
		}

		$seo_title = \SEOvaultSEO\Core\TermMeta::get_seo_title( $term_id );
		if ( '' !== $seo_title ) {
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $seo_title );
		}

		if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
			return '';
		}

		$term = get_queried_object();
		return ( $term && ! is_wp_error( $term ) ) ? (string) $term->name : '';
	}

	/**
	 * Term archive OG description (custom OG → SEO description → term description).
	 *
	 * @return string
	 */
	private static function get_term_og_description() {
		$term_id = get_queried_object_id();
		if ( ! $term_id ) {
			return '';
		}

		$og_description = \SEOvaultSEO\Core\TermMeta::get_og_description( $term_id );
		if ( '' !== $og_description ) {
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $og_description );
		}

		$seo_description = \SEOvaultSEO\Core\TermMeta::get_seo_description( $term_id );
		if ( '' !== $seo_description ) {
			return \SEOvaultSEO\ReplaceVariables\Manager::do_replace( $seo_description );
		}

		if ( ! \SEOvaultSEO\Compatibility\Detector::is_auto_generation() ) {
			return '';
		}

		$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 ) );
		}

		return '';
	}
}
