<?php
/**
 * Category badge.
 *
 * @package Kutby
 * @since   2.9.0
 */

declare(strict_types=1);

namespace Kutby\Cards\Parts;

use Kutby\Cards\Contracts\CardPartInterface;
use Kutby\Cards\Support\CardContext;
use WP_Term;

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

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

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

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

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