<?php
namespace admin\Blocks\Render;

use WP_Query;

class Single_Forum {
	/**
	 * Render the single forum Gutenberg block.
	 *
	 * Supports 4 style variations, visibility toggles, typography,
	 * spacing, color controls, responsive mobile settings, and columns.
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content    Block content.
	 * @return string Rendered block HTML.
	 */
	public function render_single_forum($attributes, $content)
	{
		// Helper to sanitize CSS color values (hex and rgba).
		$sanitize_css_color = function ($color) {
			$color = trim((string) $color);
			if (empty($color)) {
				return '';
			}
			if (preg_match('/^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$/', $color)) {
				return $color;
			}
			if (preg_match('/^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+(?:\s*,\s*(0|1|0?\.\d+))?\s*\)$/', $color)) {
				return $color;
			}
			return '';
		};

		
		$settings = $attributes;
		$settings['style']       = ! empty( $attributes['style'] )       ? $attributes['style']       : '1';

		// Enforce free defaults: styles 3 and 4 are Pro-only.
		$is_pro_active = function_exists( 'forumax_is_premium' ) && forumax_is_premium();
		if ( ! $is_pro_active && in_array( $settings['style'], array( '3', '4' ), true ) ) {
			$settings['style'] = '1';
		}
		$settings['ppp']         = ! empty( $attributes['ppp'] )         ? $attributes['ppp']         : 3;
		$settings['order']       = ! empty( $attributes['order'] )       ? $attributes['order']       : 'ASC';
		$settings['word_length'] = ! empty( $attributes['word_length'] ) ? $attributes['word_length'] : 12;
		$settings['read_more']   = ! empty( $attributes['read_more'] )   ? $attributes['read_more']   : 'View All';
		$settings['columns']     = ! empty( $attributes['columns'] )     ? (int) $attributes['columns'] : 1;

		// Visibility toggles (default true).
		$settings['show_voice']        = isset( $attributes['show_voice'] )        ? (bool) $attributes['show_voice']        : true;
		$settings['show_replies']      = isset( $attributes['show_replies'] )      ? (bool) $attributes['show_replies']      : true;
		$settings['show_freshness']    = isset( $attributes['show_freshness'] )    ? (bool) $attributes['show_freshness']    : true;
		$settings['show_excerpt']      = isset( $attributes['show_excerpt'] )      ? (bool) $attributes['show_excerpt']      : true;
		$settings['show_view_all']     = isset( $attributes['show_view_all'] )     ? (bool) $attributes['show_view_all']     : true;
		$settings['show_cover_badges'] = isset( $attributes['show_cover_badges'] ) ? (bool) $attributes['show_cover_badges'] : true;

		// Enforce visibility and responsive defaults when Pro is not active.
		if ( ! $is_pro_active ) {
			$settings['show_voice']        = true;
			$settings['show_replies']      = true;
			$settings['show_freshness']    = true;
			$settings['show_excerpt']      = true;
			$settings['show_view_all']     = true;
			$settings['show_cover_badges'] = true;
		}

		// Spacing.
		$settings['row_padding']  = isset( $attributes['row_padding'] )  ? (int) $attributes['row_padding']  : 16;
		$settings['row_gap']      = isset( $attributes['row_gap'] )      ? (int) $attributes['row_gap']      : 10;
		$settings['cover_height'] = isset( $attributes['cover_height'] ) ? (int) $attributes['cover_height'] : 250;
		$settings['border_radius'] = isset( $attributes['border_radius'] ) ? (int) $attributes['border_radius'] : 8;

		// Typography.
		$settings['title_font_size']   = ! empty( $attributes['title_font_size'] )   ? (int) $attributes['title_font_size']   : '';
		$settings['meta_font_size']    = ! empty( $attributes['meta_font_size'] )    ? (int) $attributes['meta_font_size']    : '';
		$settings['title_font_weight'] = ! empty( $attributes['title_font_weight'] ) ? $attributes['title_font_weight']       : '';

		// Cover overlay.
		$settings['cover_overlay_color'] = ! empty( $attributes['cover_overlay_color'] )
			? $sanitize_css_color( $attributes['cover_overlay_color'] )
			: 'rgba(28, 38, 56, 0.5)';

		// Responsive.
		$settings['hide_voice_mobile']   = isset( $attributes['hide_voice_mobile'] )   ? (bool) $attributes['hide_voice_mobile']   : true;
		$settings['hide_replies_mobile'] = isset( $attributes['hide_replies_mobile'] ) ? (bool) $attributes['hide_replies_mobile'] : false;
		$settings['hide_user_mobile']    = isset( $attributes['hide_user_mobile'] )    ? (bool) $attributes['hide_user_mobile']    : true;

		// Re-enforce responsive defaults for free users.
		if ( ! $is_pro_active ) {
			$settings['hide_voice_mobile']   = true;
			$settings['hide_replies_mobile'] = false;
			$settings['hide_user_mobile']    = true;
		}

		
		$title_color       = $sanitize_css_color( ! empty( $attributes['title_color'] )       ? $attributes['title_color']       : '' );
		$title_hover_color = $sanitize_css_color( ! empty( $attributes['title_hover_color'] ) ? $attributes['title_hover_color'] : '' );
		$excerpt_color     = $sanitize_css_color( ! empty( $attributes['excerpt_color'] )     ? $attributes['excerpt_color']     : '' );
		$meta_color        = $sanitize_css_color( ! empty( $attributes['meta_color'] )        ? $attributes['meta_color']        : '' );
		$bg_color          = $sanitize_css_color( ! empty( $attributes['bg_color'] )          ? $attributes['bg_color']          : '' );

		
		$forum_id = ! empty( $settings['forum_id'] ) ? $settings['forum_id'] : '';

		$cover_image = isset( $settings['cover_image'] ) ? $settings['cover_image'] : '';
		$url = '';
		if ( is_array( $cover_image ) && isset( $cover_image['url'] ) ) {
			$url = $cover_image['url'];
		} elseif ( is_object( $cover_image ) && isset( $cover_image->url ) ) {
			$url = $cover_image->url;
		}
		$post_thumbnail_url = ! empty( $url ) ? $url : get_the_post_thumbnail_url( $forum_id );

		$topics = new \WP_Query( array(
			'post_type'      => bbp_get_topic_post_type(),
			'order'          => $settings['order'],
			'posts_per_page' => $settings['ppp'],
			'post_parent'    => $forum_id,
		) );

		$unique_id = uniqid( 'bbpc-single-forum-' );

		
		if ( ! wp_style_is( 'bbpc-el-widgets', 'registered' ) ) {
			wp_register_style( 'bbpc-el-widgets', FORUMAX_STYLES . 'frontend/el-widgets.css' );
		}
		wp_enqueue_style( 'bbpc-el-widgets' );

		
		ob_start();
		echo '<div id="' . esc_attr( $unique_id ) . '" class="bbpc-single-forum-block">';

		if ( $forum_id ) {
			$style_num     = in_array( $settings['style'], array( '1', '2', '3', '4' ), true ) ? $settings['style'] : '1';
			$template_path = __DIR__ . '/../../Elementor/inc/single-forum/single-forum-' . $style_num . '.php';
			if ( file_exists( $template_path ) ) {
				include $template_path;
			}
		} else {
			?>
			<div class="bbpc-sf-empty-placeholder" style="border:2px dashed #ccc;border-radius:8px;padding:40px 20px;text-align:center;color:#888;">
				<span class="dashicons dashicons-admin-post" style="font-size:48px;width:48px;height:48px;margin-bottom:10px;display:block;margin-left:auto;margin-right:auto;"></span>
				<p style="font-size:15px;margin:0;"><?php esc_html_e( 'Please select a forum from the block settings.', 'forumax' ); ?></p>
			</div>
			<?php
		}

		echo '</div>';

		
		$row_padding   = (int) $settings['row_padding'];
		$row_gap       = (int) $settings['row_gap'];
		$cover_height  = (int) $settings['cover_height'];
		$border_radius = (int) $settings['border_radius'];
		?>
		<style>
			
			#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents {
				padding: <?php echo esc_attr( $row_padding ); ?>px 30px !important;
				margin: <?php echo esc_attr( $row_gap ); ?>px 0 !important;
				border-radius: <?php echo esc_attr( $border_radius ); ?>px !important;
			}
			#<?php echo esc_attr( $unique_id ); ?> .forum-thumbnail {
				min-height: <?php echo esc_attr( $cover_height ); ?>px !important;
			}
			#<?php echo esc_attr( $unique_id ); ?> .forum-card {
				border-radius: <?php echo esc_attr( $border_radius ); ?>px !important;
				overflow: hidden;
			}
			#<?php echo esc_attr( $unique_id ); ?> .forum-thumbnail {
				border-radius: <?php echo esc_attr( $border_radius ); ?>px <?php echo esc_attr( $border_radius ); ?>px 0 0 !important;
			}
			<?php if ( $settings['style'] === '3' ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item {
					padding: <?php echo esc_attr( $row_padding ); ?>px 20px !important;
					margin-bottom: <?php echo esc_attr( $row_gap ); ?>px !important;
					border-radius: <?php echo esc_attr( $border_radius ); ?>px !important;
				}
			<?php endif; ?>
			<?php if ( $settings['style'] === '4' ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid {
					border-radius: <?php echo esc_attr( $border_radius ); ?>px !important;
				}
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid-topic {
					padding: <?php echo esc_attr( $row_padding ); ?>px 24px !important;
				}
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid-info {
					min-height: <?php echo esc_attr( $cover_height ); ?>px !important;
				}
			<?php endif; ?>

			
			<?php if ( $title_color ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3,
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3 a,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3 a,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title a,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 a {
					color: <?php echo esc_attr( $title_color ); ?> !important;
				}
			<?php endif; ?>

			<?php if ( $title_hover_color ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3:hover,
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3 a:hover,
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3:hover a,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3:hover,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3 a:hover,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3:hover a,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title a:hover,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3:hover,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 a:hover,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3:hover a {
					color: <?php echo esc_attr( $title_hover_color ); ?> !important;
				}
			<?php endif; ?>

			<?php if ( $excerpt_color ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title p,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-body,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-excerpt,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic p {
					color: <?php echo esc_attr( $excerpt_color ); ?> !important;
				}
			<?php endif; ?>

			<?php if ( $meta_color ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .voice,
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .topics,
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user .user-info .date,
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user .user-info .date a,
				#<?php echo esc_attr( $unique_id ); ?> .topic-heading .icon-text,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-meta span,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-meta span {
					color: <?php echo esc_attr( $meta_color ); ?> !important;
				}
			<?php endif; ?>

			<?php if ( $bg_color ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-body,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic {
					background-color: <?php echo esc_attr( $bg_color ); ?> !important;
				}
			<?php endif; ?>

			
			<?php if ( $settings['title_font_size'] ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .title h3,
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .title h3 a,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 {
					font-size: <?php echo esc_attr( $settings['title_font_size'] ); ?>px !important;
				}
			<?php endif; ?>

			<?php if ( $settings['title_font_weight'] ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .title h3,
				#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 {
					font-weight: <?php echo esc_attr( $settings['title_font_weight'] ); ?> !important;
				}
			<?php endif; ?>

			<?php if ( $settings['meta_font_size'] ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .voice,
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .topics,
				#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user .user-info .date,
				#<?php echo esc_attr( $unique_id ); ?> .topic-heading .icon-text,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-meta,
				#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-meta {
					font-size: <?php echo esc_attr( $settings['meta_font_size'] ); ?>px !important;
				}
			<?php endif; ?>

			
			@media all and (max-width: 560px) {
				<?php if ( $settings['hide_voice_mobile'] ) : ?>
					#<?php echo esc_attr( $unique_id ); ?> .topic-contents .voice,
					#<?php echo esc_attr( $unique_id ); ?> .topic-heading .voice {
						display: none !important;
					}
				<?php endif; ?>
				<?php if ( $settings['hide_replies_mobile'] ) : ?>
					#<?php echo esc_attr( $unique_id ); ?> .topic-contents .topics,
					#<?php echo esc_attr( $unique_id ); ?> .topic-heading .replies {
						display: none !important;
					}
				<?php endif; ?>
				<?php if ( $settings['hide_user_mobile'] ) : ?>
					#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user,
					#<?php echo esc_attr( $unique_id ); ?> .topic-heading .freshness {
						display: none !important;
					}
				<?php endif; ?>
			}
		</style>
		<?php

		return ob_get_clean();
	}
}