<?php
/**
 * Admin UI Design System — reusable components (markup only).
 * Does not change Arabic copy; callers pass Arabic strings.
 *
 * @package Kutby
 * @since   2.5.0
 */

declare(strict_types=1);

namespace Kutby\Admin\UI;

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

/**
 * Class Components
 */
final class Components {

	/**
	 * Panel shell opening.
	 *
	 * @param string $title Title (Arabic).
	 * @param string $lead  Optional lead.
	 * @return void
	 */
	public static function panel_start( string $title, string $lead = '' ): void {
		echo '<div class="kutby-panel">';
		echo '<header class="kutby-panel__header">';
		echo '<h2>' . esc_html( $title ) . '</h2>';
		if ( '' !== $lead ) {
			echo '<p class="kutby-panel__lead">' . esc_html( $lead ) . '</p>';
		}
		echo '</header>';
	}

	/**
	 * Panel shell closing.
	 *
	 * @return void
	 */
	public static function panel_end(): void {
		echo '</div>';
	}

	/**
	 * Setting card row.
	 *
	 * @param string $label   Label.
	 * @param string $control Control HTML (already escaped by renderer).
	 * @param string $hint    Optional hint.
	 * @param string $for     Optional label for attribute.
	 * @return void
	 */
	public static function card( string $label, string $control, string $hint = '', string $for = '' ): void {
		echo '<div class="kutby-card">';
		echo '<div class="kutby-card__meta">';
		echo '<label class="kutby-card__label"' . ( $for ? ' for="' . esc_attr( $for ) . '"' : '' ) . '>' . esc_html( $label ) . '</label>';
		if ( '' !== $hint ) {
			echo '<p class="kutby-card__hint">' . esc_html( $hint ) . '</p>';
		}
		echo '</div><div class="kutby-card__control">';
		echo $control; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field renderers escape.
		echo '</div></div>';
	}

	/**
	 * Primary / secondary button.
	 *
	 * @param string               $label Label.
	 * @param array<string, mixed> $attrs Attributes (type, class, name, id, value).
	 * @return string
	 */
	public static function button( string $label, array $attrs = array() ): string {
		$type    = (string) ( $attrs['type'] ?? 'button' );
		$class   = 'kutby-btn ' . (string) ( $attrs['class'] ?? 'kutby-btn--primary' );
		$loading = ! empty( $attrs['loading'] );
		$extra   = '';

		foreach ( array( 'name', 'id', 'value', 'data-action' ) as $key ) {
			if ( isset( $attrs[ $key ] ) ) {
				$extra .= sprintf( ' %s="%s"', $key, esc_attr( (string) $attrs[ $key ] ) );
			}
		}
		if ( ! empty( $attrs['hidden'] ) ) {
			$extra .= ' hidden';
		}
		if ( ! empty( $attrs['disabled'] ) || $loading ) {
			$extra .= ' disabled';
		}
		if ( $loading ) {
			$class .= ' is-loading';
		}

		$content = $loading
			? self::loader() . ' <span>' . esc_html( $label ) . '</span>'
			: esc_html( $label );

		return sprintf(
			'<button type="%1$s" class="%2$s"%3$s>%4$s</button>',
			esc_attr( $type ),
			esc_attr( trim( $class ) ),
			$extra,
			$content
		);
	}

	/**
	 * Text input.
	 *
	 * @param array<string, mixed> $attrs Attributes.
	 * @return string
	 */
	public static function input( array $attrs = array() ): string {
		$type = (string) ( $attrs['type'] ?? 'text' );
		$extra = '';
		if ( ! empty( $attrs['disabled'] ) ) {
			$extra .= ' disabled';
		}
		if ( ! empty( $attrs['placeholder'] ) ) {
			$extra .= ' placeholder="' . esc_attr( (string) $attrs['placeholder'] ) . '"';
		}

		return sprintf(
			'<input type="%1$s" class="kutby-field kutby-field--%1$s %2$s" id="%3$s" name="%4$s" value="%5$s"%6$s />',
			esc_attr( $type ),
			esc_attr( (string) ( $attrs['class'] ?? '' ) ),
			esc_attr( (string) ( $attrs['id'] ?? '' ) ),
			esc_attr( (string) ( $attrs['name'] ?? '' ) ),
			esc_attr( (string) ( $attrs['value'] ?? '' ) ),
			$extra
		);
	}

	/**
	 * Textarea.
	 *
	 * @param array<string, mixed> $attrs Attributes.
	 * @return string
	 */
	public static function textarea( array $attrs = array() ): string {
		return sprintf(
			'<textarea class="kutby-field kutby-field--textarea" id="%1$s" name="%2$s" rows="%3$d">%4$s</textarea>',
			esc_attr( (string) ( $attrs['id'] ?? '' ) ),
			esc_attr( (string) ( $attrs['name'] ?? '' ) ),
			(int) ( $attrs['rows'] ?? 4 ),
			esc_textarea( (string) ( $attrs['value'] ?? '' ) )
		);
	}

	/**
	 * Select.
	 *
	 * @param array<string, mixed> $attrs Attributes (options, value, id, name).
	 * @return string
	 */
	public static function select( array $attrs = array() ): string {
		$options = is_array( $attrs['options'] ?? null ) ? $attrs['options'] : array();
		$current = (string) ( $attrs['value'] ?? '' );
		$html    = sprintf(
			'<select class="kutby-field kutby-field--select" id="%1$s" name="%2$s">',
			esc_attr( (string) ( $attrs['id'] ?? '' ) ),
			esc_attr( (string) ( $attrs['name'] ?? '' ) )
		);
		foreach ( $options as $val => $label ) {
			$html .= sprintf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( (string) $val ),
				selected( $current, (string) $val, false ),
				esc_html( (string) $label )
			);
		}
		$html .= '</select>';
		return $html;
	}

	/**
	 * Toggle switch.
	 *
	 * @param array<string, mixed> $attrs Attributes.
	 * @return string
	 */
	public static function switch( array $attrs = array() ): string {
		return sprintf(
			'<label class="kutby-toggle"><input type="checkbox" id="%1$s" name="%2$s" value="1" %3$s /><span class="kutby-toggle__track" aria-hidden="true"></span><span class="kutby-toggle__label">%4$s</span></label>',
			esc_attr( (string) ( $attrs['id'] ?? '' ) ),
			esc_attr( (string) ( $attrs['name'] ?? '' ) ),
			checked( ! empty( $attrs['value'] ), true, false ),
			esc_html( (string) ( $attrs['label'] ?? '' ) )
		);
	}

	/**
	 * Notice / alert.
	 *
	 * @param string $message Message.
	 * @param string $type    success|error|warning|info.
	 * @param bool   $dismiss Dismissible.
	 * @param bool   $autohide Auto-hide.
	 * @return void
	 */
	public static function notice( string $message, string $type = 'success', bool $dismiss = false, bool $autohide = false ): void {
		$class = 'kutby-notice kutby-notice--' . sanitize_html_class( $type );
		if ( $dismiss ) {
			$class .= ' is-dismissible';
		}
		if ( $autohide ) {
			$class .= ' is-autohide';
		}
		printf(
			'<div class="%1$s" role="status" data-kutby-notice>%2$s%3$s</div>',
			esc_attr( $class ),
			esc_html( $message ),
			$dismiss ? '<button type="button" class="kutby-notice__dismiss" aria-label="إغلاق">&times;</button>' : ''
		);
	}

	/**
	 * Alert alias.
	 *
	 * @param string $message Message.
	 * @param string $type    Type.
	 * @return void
	 */
	public static function alert( string $message, string $type = 'info' ): void {
		self::notice( $message, $type, true );
	}

	/**
	 * Badge.
	 *
	 * @param string $label Label.
	 * @return string
	 */
	public static function badge( string $label ): string {
		return '<span class="kutby-badge">' . esc_html( $label ) . '</span>';
	}

	/**
	 * Sidebar shell start (admin nav container).
	 *
	 * @param string $label Aria label.
	 * @return void
	 */
	public static function sidebar_start( string $label = '' ): void {
		printf(
			'<aside class="kutby-admin__sidebar" aria-label="%s">',
			esc_attr( $label )
		);
	}

	/**
	 * Sidebar shell end.
	 *
	 * @return void
	 */
	public static function sidebar_end(): void {
		echo '</aside>';
	}

	/**
	 * Dropdown shell.
	 *
	 * @param string $trigger Trigger HTML.
	 * @param string $menu    Menu HTML.
	 * @return string
	 */
	public static function dropdown( string $trigger, string $menu ): string {
		return '<div class="kutby-ui-dropdown" data-kutby-dropdown>' .
			'<div class="kutby-ui-dropdown__trigger">' . $trigger . '</div>' .
			'<div class="kutby-ui-dropdown__menu" hidden>' . $menu . '</div></div>';
	}

	/**
	 * Accordion item.
	 *
	 * @param string $title   Title.
	 * @param string $content Content HTML.
	 * @param bool   $open    Open by default.
	 * @return string
	 */
	public static function accordion( string $title, string $content, bool $open = false ): string {
		return sprintf(
			'<details class="kutby-ui-accordion"%1$s><summary>%2$s</summary><div class="kutby-ui-accordion__body">%3$s</div></details>',
			$open ? ' open' : '',
			esc_html( $title ),
			$content // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- caller owns escaping.
		);
	}

	/**
	 * Modal shell (hidden by default).
	 *
	 * @param string $id      Modal id.
	 * @param string $title   Title.
	 * @param string $content Body HTML.
	 * @return string
	 */
	public static function modal( string $id, string $title, string $content ): string {
		return sprintf(
			'<div class="kutby-ui-modal" id="%1$s" data-kutby-modal hidden><div class="kutby-ui-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="%1$s-title"><h3 id="%1$s-title">%2$s</h3><div class="kutby-ui-modal__body">%3$s</div><button type="button" class="kutby-btn kutby-btn--ghost" data-kutby-modal-close>إغلاق</button></div></div>',
			esc_attr( $id ),
			esc_html( $title ),
			$content // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- caller owns escaping.
		);
	}

	/**
	 * Media upload control shell.
	 *
	 * @param array<string, mixed> $attrs Attributes.
	 * @return string
	 */
	public static function media_upload( array $attrs = array() ): string {
		$id    = (int) ( $attrs['value'] ?? 0 );
		$url   = $id ? wp_get_attachment_image_url( $id, 'medium' ) : '';
		$label = $url ? (string) ( $attrs['change_label'] ?? 'تعديل' ) : (string) ( $attrs['select_label'] ?? 'اختيار صورة' );

		$html  = '<div class="kutby-media-field">';
		$html .= sprintf(
			'<input type="hidden" class="kutby-media-field__input" id="%1$s" name="%2$s" value="%3$d" />',
			esc_attr( (string) ( $attrs['id'] ?? '' ) ),
			esc_attr( (string) ( $attrs['name'] ?? '' ) ),
			$id
		);
		$html .= sprintf(
			'<div class="kutby-media-field__preview">%s</div>',
			$url ? '<img src="' . esc_url( $url ) . '" alt="" />' : ''
		);
		$html .= self::button( $label, array( 'class' => 'kutby-btn--secondary kutby-media-field__select' ) );
		$html .= ' ';
		$html .= self::button(
			(string) ( $attrs['remove_label'] ?? 'حذف' ),
			array(
				'class'  => 'kutby-btn--ghost kutby-media-field__remove',
				'hidden' => ! $id,
			)
		);
		$html .= '</div>';

		return $html;
	}

	/**
	 * Tooltip wrapper.
	 *
	 * @param string $content Visible content HTML.
	 * @param string $tip     Tip text.
	 * @return string
	 */
	public static function tooltip( string $content, string $tip ): string {
		return sprintf(
			'<span class="kutby-ui-tooltip" data-tip="%1$s">%2$s</span>',
			esc_attr( $tip ),
			$content // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- caller owns escaping.
		);
	}

	/**
	 * Skeleton placeholder.
	 *
	 * @param int $lines Lines.
	 * @return void
	 */
	public static function skeleton( int $lines = 3 ): void {
		echo '<div class="kutby-skeleton" aria-hidden="true">';
		for ( $i = 0; $i < $lines; $i++ ) {
			echo '<div class="kutby-skeleton__line"></div>';
		}
		echo '</div>';
	}

	/**
	 * Loader.
	 *
	 * @return string
	 */
	public static function loader(): string {
		return '<span class="kutby-loader" aria-hidden="true"></span>';
	}

	/**
	 * Tabs nav (markup only).
	 *
	 * @param array<string, string> $tabs   id => label.
	 * @param string                $active Active id.
	 * @return void
	 */
	public static function tabs( array $tabs, string $active = '' ): void {
		echo '<div class="kutby-ui-tabs" role="tablist">';
		foreach ( $tabs as $id => $label ) {
			$class = 'kutby-ui-tabs__item' . ( $id === $active ? ' is-active' : '' );
			printf(
				'<button type="button" class="%1$s" role="tab" data-kutby-tab="%2$s">%3$s</button>',
				esc_attr( $class ),
				esc_attr( $id ),
				esc_html( $label )
			);
		}
		echo '</div>';
	}
}
