<?php
/**
 * Category badge for a post.
 *
 * @package Kutby
 * @since   2.8.0
 */

declare(strict_types=1);

namespace Kutby\Hero\Components;

use Kutby\Hero\Contracts\HeroComponentInterface;
use Kutby\Hero\Support\HeroContext;
use WP_Post;
use WP_Term;

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

/**
 * Class CategoryBadge
 */
final class CategoryBadge implements HeroComponentInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function render( HeroContext $ctx, array $args = array() ): void {
		if ( ! $ctx->bool( 'show_category' ) ) {
			return;
		}

		$post = $args['post'] ?? null;
		if ( ! $post instanceof WP_Post ) {
			return;
		}

		$terms = get_the_category( $post->ID );
		if ( empty( $terms ) || ! $terms[0] instanceof WP_Term ) {
			return;
		}

		$term = $terms[0];
		?>
		<a class="kutby-hero-badge" href="<?php echo esc_url( get_category_link( $term ) ); ?>">
			<?php echo esc_html( $term->name ); ?>
		</a>
		<?php
	}
}
