<?php
/**
 * Notification Renderer - FAB + Slide Panel
 *
 * @package KNS_Dynamic_Discounts
 */

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

/**
 * Renders notification UI components
 */
class KNSDD_Notification_Renderer {

	/**
	 * Render Floating Action Button
	 *
	 * @param int $unread_count Number of unread notifications
	 */
	public function render_fab( $unread_count ) {
		?>
		<!-- Floating Action Button -->
		<div id="knsdd-notification-fab" class="knsdd-fab" data-unread="<?php echo esc_attr( $unread_count ); ?>">
			<button type="button" class="knsdd-fab-button" aria-label="<?php esc_attr_e( 'Bildirimleri Aç', 'kns-dynamic-discounts-for-woocommerce' ); ?>">
				<svg class="knsdd-fab-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
					<path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path>
					<polyline points="22,6 12,13 2,6"></polyline>
				</svg>
				<?php if ( $unread_count > 0 ) : ?>
					<span class="knsdd-fab-badge"><?php echo esc_html( $unread_count ); ?></span>
				<?php endif; ?>
			</button>
		</div>

		<!-- Slide Panel -->
		<div id="knsdd-notification-panel" class="knsdd-notification-panel">
			<div class="knsdd-panel-header">
				<h3><?php esc_html_e( 'Bildirimler', 'kns-dynamic-discounts-for-woocommerce' ); ?></h3>
				<button type="button" class="knsdd-panel-close" aria-label="<?php esc_attr_e( 'Kapat', 'kns-dynamic-discounts-for-woocommerce' ); ?>">
					<span aria-hidden="true">×</span>
				</button>
			</div>
			<div class="knsdd-panel-body">
				<div class="knsdd-notifications-loading">
					<span class="spinner is-active"></span>
					<p><?php esc_html_e( 'Yükleniyor...', 'kns-dynamic-discounts-for-woocommerce' ); ?></p>
				</div>
				<div class="knsdd-notifications-list" style="display: none;"></div>
				<div class="knsdd-notifications-empty" style="display: none;">
					<p><?php esc_html_e( 'Bildirim bulunmuyor', 'kns-dynamic-discounts-for-woocommerce' ); ?></p>
				</div>
				<div class="knsdd-notifications-error" style="display: none;">
					<p><?php esc_html_e( 'Bildirimler yüklenirken hata oluştu', 'kns-dynamic-discounts-for-woocommerce' ); ?></p>
				</div>
			</div>
			<div class="knsdd-panel-footer" style="display: none;">
				<button type="button" class="button knsdd-load-more">
					<?php esc_html_e( 'Daha Fazla Yükle', 'kns-dynamic-discounts-for-woocommerce' ); ?>
					<span class="knsdd-remaining-count"></span>
				</button>
			</div>
		</div>

		<!-- Overlay -->
		<div id="knsdd-notification-overlay" class="knsdd-notification-overlay"></div>
		<?php
	}

	/**
	 * Render single notification item (used by JavaScript)
	 *
	 * @param array $notification Notification data
	 * @return string HTML markup
	 */
	public static function render_notification_item( $notification ) {
		$id       = isset( $notification['id'] ) ? esc_attr( $notification['id'] ) : '';
		$icon     = isset( $notification['icon'] ) ? $notification['icon'] : '📢';
		$message  = isset( $notification['message'] ) ? esc_html( $notification['message'] ) : '';
		$actions  = isset( $notification['actions'] ) && is_array( $notification['actions'] ) ? $notification['actions'] : array();
		$priority = isset( $notification['priority'] ) ? (int) $notification['priority'] : 50;

		ob_start();
		?>
		<div class="knsdd-notification-item" data-id="<?php echo esc_attr( $id ); ?>" data-priority="<?php echo esc_attr( $priority ); ?>">
			<div class="knsdd-notification-content">
				<span class="knsdd-notification-icon"><?php echo wp_kses_post( $icon ); ?></span>
				<span class="knsdd-notification-message"><?php echo esc_html( $message ); ?></span>
			</div>
			<div class="knsdd-notification-actions">
				<?php foreach ( $actions as $action ) : ?>
					<?php
					$label = isset( $action['label'] ) ? esc_html( $action['label'] ) : '';
					$url   = isset( $action['url'] ) ? esc_url( $action['url'] ) : '';
					$type  = isset( $action['type'] ) ? esc_attr( $action['type'] ) : 'secondary';
					?>
					<?php if ( ! empty( $url ) ) : ?>
						<a href="<?php echo esc_url( $url ); ?>" class="button button-<?php echo esc_attr( $type ); ?>">
							<?php echo esc_html( $label ); ?>
						</a>
					<?php endif; ?>
				<?php endforeach; ?>
				<button type="button" class="knsdd-dismiss-notification" data-id="<?php echo esc_attr( $id ); ?>" aria-label="<?php esc_attr_e( 'Gizle', 'kns-dynamic-discounts-for-woocommerce' ); ?>"></button>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
