<?php
/**
 * Badge component view.
 *
 * @package Awesomemotive\Rewardswp\Views\Admin\Components
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Admin\Components;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use function Awesomemotive\Rewardswp\Helpers\tag_attr;


/**
 * Badge Component View.
 *
 * @since 1.0.0
 */
class BadgeComponentView extends AbstractView implements ViewInterface {
	/**
	 * Render the template.
	 *
	 * @since 1.1.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'text'               => '',
				'style'              => '',
				'additional_classes' => '',
				'x-show'             => '',
			]
		);

		// Return early if style is not provided or invalid.
		if ( empty( $args['style'] ) || ! in_array( $args['style'], [ 'pro', 'active', 'inactive', 'warning' ], true ) ) {
			return;
		}

		// Auto-set text based on style if not provided.
		if ( empty( $args['text'] ) ) {
			switch ( $args['style'] ) {
				case 'pro':
					$args['text'] = esc_html__( 'Pro', 'rewardswp' );
					break;
				case 'active':
					$args['text'] = esc_html__( 'Active', 'rewardswp' );
					break;
				case 'inactive':
					$args['text'] = esc_html__( 'Inactive', 'rewardswp' );
					break;
				case 'warning':
					$args['text'] = esc_html__( 'Warning', 'rewardswp' );
					break;
				default:
					// Return early if text is empty and style doesn't have a default.
					return;
			}
		}

		// Base classes for all badges.
		$base_classes = 'inline-flex items-center justify-center px-2 rounded-full font-medium flex-shrink-0 leading-none';

		// Style-specific classes.
		$style_classes = '';
		$text_content  = $args['text'];

		switch ( $args['style'] ) {
			case 'pro':
				$style_classes = 'py-1 text-[10px] bg-gray-200 text-gray-500';
				$text_content  = sprintf( '<span class="uppercase tracking-wide leading-none">%s</span>', esc_html( $args['text'] ) );
				break;

			case 'active':
				$style_classes = 'py-1 text-xs bg-green-100 text-green-800';
				break;

			case 'inactive':
				$style_classes = 'py-1 text-xs bg-red-100 text-red-800';
				break;

			case 'warning':
				$style_classes = 'py-1 text-xs bg-amber-100 text-amber-800';
				break;
		}

		// Combine all classes.
		$classes = trim( $base_classes . ' ' . $style_classes . ' ' . $args['additional_classes'] );

		// Build x-show attribute if provided.
		echo '<span class="' . esc_attr( $classes ) . '"';
		tag_attr( 'x-show', $args['x-show'] );
		echo '>';
		if ( 'pro' === $args['style'] ) {
			echo wp_kses_post( $text_content );
		} else {
			echo esc_html( $args['text'] );
		}
		echo '</span>';
	}
}
