<?php
namespace ZS_SEO\AuditOptimization\Core;

defined( 'ABSPATH' ) || exit;

class MetaTags {

    public function __construct() {
        // Remove WordPress default title tag handling so we control it
        add_theme_support( 'title-tag' );
        add_filter( 'pre_get_document_title', [ $this, 'get_title'       ], 20 );
        add_action( 'wp_head',                [ $this, 'inject_meta'     ], 1  );
        add_action( 'wp_head',                [ $this, 'inject_og_tags'  ], 2  );
        add_action( 'wp_head',                [ $this, 'inject_canonical' ], 3 );
    }

    // ── Title ─────────────────────────────────────────────────────────────────
    public function get_title( string $title ): string {
        if ( is_singular() ) {
            $post_id    = get_the_ID();
            $custom     = get_post_meta( $post_id, '_zs_seo_title', true );
            if ( $custom ) return esc_html( $custom );
        }
        return $title;
    }

    // ── Meta description + robots ──────────────────────────────────────────────
    public function inject_meta(): void {
        if ( is_singular() ) {
            $post_id = get_the_ID();

            // Description
            $desc = get_post_meta( $post_id, '_zs_seo_description', true );
            if ( ! $desc ) {
                $desc = wp_trim_words( wp_strip_all_tags( get_the_excerpt( $post_id ) ), 30 );
            }
            if ( $desc ) {
                printf( '<meta name="description" content="%s" />' . "\n", esc_attr( $desc ) );
            }

            // Robots
            $noindex   = get_post_meta( $post_id, '_zs_seo_noindex',   true );
            $nofollow  = get_post_meta( $post_id, '_zs_seo_nofollow',  true );
            $robots    = [];
            $robots[]  = $noindex  ? 'noindex'  : 'index';
            $robots[]  = $nofollow ? 'nofollow' : 'follow';
            printf( '<meta name="robots" content="%s" />' . "\n", esc_attr( implode( ', ', $robots ) ) );
        }
    }

    // ── Open Graph + Twitter Card ──────────────────────────────────────────────
    public function inject_og_tags(): void {
        if ( ! is_singular() ) return;

        $post_id  = get_the_ID();
        $post     = get_post( $post_id );
        $title    = get_post_meta( $post_id, '_zs_seo_og_title', true ) ?: get_the_title();
        $desc     = get_post_meta( $post_id, '_zs_seo_description', true )
                    ?: wp_trim_words( wp_strip_all_tags( $post->post_content ?? '' ), 30 );
        $url      = get_permalink( $post_id );
        $image    = get_the_post_thumbnail_url( $post_id, 'large' );
        $sitename = get_bloginfo( 'name' );

        echo '<meta property="og:type"        content="article" />' . "\n";
        printf( '<meta property="og:title"       content="%s" />' . "\n", esc_attr( $title ) );
        printf( '<meta property="og:description" content="%s" />' . "\n", esc_attr( $desc ) );
        printf( '<meta property="og:url"         content="%s" />' . "\n", esc_url( $url ) );
        printf( '<meta property="og:site_name"   content="%s" />' . "\n", esc_attr( $sitename ) );
        if ( $image ) {
            printf( '<meta property="og:image"       content="%s" />' . "\n", esc_url( $image ) );
        }

        // Twitter card
        echo '<meta name="twitter:card"        content="summary_large_image" />' . "\n";
        printf( '<meta name="twitter:title"       content="%s" />' . "\n", esc_attr( $title ) );
        printf( '<meta name="twitter:description" content="%s" />' . "\n", esc_attr( $desc ) );
        if ( $image ) {
            printf( '<meta name="twitter:image"      content="%s" />' . "\n", esc_url( $image ) );
        }
    }

    // ── Canonical ─────────────────────────────────────────────────────────────
    public function inject_canonical(): void {
        if ( is_singular() ) {
            $post_id  = get_the_ID();
            $custom   = get_post_meta( $post_id, '_zs_seo_canonical', true );
            $url      = $custom ?: get_permalink( $post_id );
            printf( '<link rel="canonical" href="%s" />' . "\n", esc_url( $url ) );
        } elseif ( is_home() || is_front_page() ) {
            printf( '<link rel="canonical" href="%s" />' . "\n", esc_url( home_url( '/' ) ) );
        }
    }
}
