<?php
/**
 * SEO / Meta-tags class.
 *
 * Outputs Open Graph and Twitter Card meta tags.
 * Does NOT run if a dedicated SEO plugin (Yoast, RankMath, AIOSEO) is active.
 *
 * @package CyyWordpress
 */

if ( ! class_exists( 'CyyWordpress_SEO' ) ) :

	/**
	 * Manages Open Graph + Twitter Card meta output.
	 */
	class CyyWordpress_SEO {

		/**
		 * Register hooks.
		 */
		public static function init(): void {
			// Bail early if a popular SEO plugin is active.
			if (
				defined( 'WPSEO_VERSION' )     // Yoast SEO
				|| defined( 'RANKMATH_VERSION' ) // RankMath
				|| class_exists( 'All_in_One_SEO_Pack' )
			) {
				return;
			}

			add_action( 'wp_head', array( static::class, 'output_meta_tags' ), 5 );
		}

		/**
		 * Output all meta tags.
		 */
		public static function output_meta_tags(): void {
			$description = self::get_description();
			$image       = self::get_image();
			$type        = is_singular() ? 'article' : 'website';
			$url         = esc_url( self::get_canonical_url() );
			$title       = esc_attr( wp_get_document_title() );

			echo "\n<!-- CyyWordpress SEO Meta -->\n";

			// Open Graph.
			printf( '<meta property="og:title" content="%s">' . PHP_EOL,       esc_attr( $title ) );
			printf( '<meta property="og:description" content="%s">' . PHP_EOL, esc_attr( $description ) );
			printf( '<meta property="og:url" content="%s">' . PHP_EOL,         $url );
			printf( '<meta property="og:type" content="%s">' . PHP_EOL,        esc_attr( $type ) );
			printf( '<meta property="og:site_name" content="%s">' . PHP_EOL,   esc_attr( get_bloginfo( 'name' ) ) );

			if ( $image ) {
				printf( '<meta property="og:image" content="%s">' . PHP_EOL,         esc_url( $image ) );
				printf( '<meta property="og:image:alt" content="%s">' . PHP_EOL,     esc_attr( $title ) );
			}

			// Twitter Card.
			$card_type = $image ? 'summary_large_image' : 'summary';
			printf( '<meta name="twitter:card" content="%s">' . PHP_EOL,        esc_attr( $card_type ) );
			printf( '<meta name="twitter:title" content="%s">' . PHP_EOL,       esc_attr( $title ) );
			printf( '<meta name="twitter:description" content="%s">' . PHP_EOL, esc_attr( $description ) );

			if ( $image ) {
				printf( '<meta name="twitter:image" content="%s">' . PHP_EOL, esc_url( $image ) );
			}

			echo "<!-- /CyyWordpress SEO Meta -->\n\n";
		}

		/**
		 * Returns a cleaned excerpt / description.
		 */
		private static function get_description(): string {
			if ( is_singular() ) {
				global $post;
				$excerpt = has_excerpt() ? get_the_excerpt() : wp_trim_words( wp_strip_all_tags( $post->post_content ), 30 );
				return sanitize_text_field( $excerpt );
			}

			$tagline = get_bloginfo( 'description' );
			return sanitize_text_field( $tagline );
		}

		/**
		 * Returns the OG image URL.
		 */
		private static function get_image(): string {
			if ( is_singular() && has_post_thumbnail() ) {
				$src = wp_get_attachment_image_src( (int) get_post_thumbnail_id(), 'large' );
				if ( $src ) {
					return $src[0];
				}
			}

			// Site-wide fallback: custom logo.
			$logo_id = get_theme_mod( 'custom_logo' );
			if ( $logo_id ) {
				$src = wp_get_attachment_image_src( (int) $logo_id, 'full' );
				if ( $src ) {
					return $src[0];
				}
			}

			return '';
		}

		/**
		 * Returns the canonical URL for the current page.
		 */
		private static function get_canonical_url(): string {
			if ( is_singular() ) {
				return (string) get_permalink();
			}

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

			return home_url( add_query_arg( array() ) );
		}
	}

endif;
