<?php
namespace WPLens\Schema\Generators;

if ( ! defined( 'ABSPATH' ) ) { exit; }

final class ImageGenerator implements \WPLens\Schema\Generator {

	public function get_slug(): string  { return 'image'; }
	public function get_label(): string { return __( 'ImageObject (featured image)', 'lens-ai-visibility-monitor' ); }

	public function applies( ?\WP_Post $post = null ): bool {
		return $post instanceof \WP_Post && has_post_thumbnail( $post->ID );
	}

	public function generate( ?\WP_Post $post = null ): ?array {
		if ( ! $post ) { return null; }

		$id = (int) get_post_thumbnail_id( $post->ID );
		if ( $id < 1 ) { return null; }

		$meta = wp_get_attachment_metadata( $id );
		$src  = wp_get_attachment_image_url( $id, 'full' );
		if ( ! $src ) { return null; }

		$alt = (string) get_post_meta( $id, '_wp_attachment_image_alt', true );

		$payload = array(
			'@type'  => 'ImageObject',
			'@id'    => $src . '#featured',
			'url'    => $src,
		);
		if ( ! empty( $meta['width'] ) )  { $payload['width']  = (int) $meta['width']; }
		if ( ! empty( $meta['height'] ) ) { $payload['height'] = (int) $meta['height']; }
		if ( '' !== $alt ) {
			$payload['caption'] = $alt;
			$payload['name']    = $alt;
		}
		return $payload;
	}
}
