<?php
/**
 * Card facade — render a registered variant.
 *
 * @package Kutby
 * @since   2.9.0
 */

declare(strict_types=1);

namespace Kutby\Cards;

use Kutby\Cards\Contracts\CardVariantInterface;
use Kutby\Cards\Services\CardRegistry;
use Kutby\Cards\Support\CardContext;
use Kutby\Cards\Variants\GridCard;
use WP_Post;

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

/**
 * Class Card
 */
final class Card {

	public const DEFAULT_VARIANT = 'grid';

	/**
	 * Render a card variant for a post.
	 *
	 * @param string               $variant Variant id.
	 * @param WP_Post|int          $post    Post.
	 * @param array<string, mixed> $args    Display args.
	 * @return void
	 */
	public static function render( string $variant, WP_Post|int $post, array $args = array() ): void {
		$ctx = CardContext::from_post( $post, $args );
		if ( null === $ctx ) {
			return;
		}

		$id    = sanitize_key( $variant );
		$class = CardRegistry::instance()->get( $id );

		if ( null === $class || ! is_subclass_of( $class, CardVariantInterface::class ) ) {
			$class = CardRegistry::instance()->get( self::DEFAULT_VARIANT ) ?? GridCard::class;
		}

		/** @var class-string<CardVariantInterface> $class */
		$class::render( $ctx );
	}
}
