<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}

if ( ! gigneo_user_have_access() ) {
	return;
}

$notices = gigneo_get_notices();

if ( ! empty( $notices ) ) { ?>
	<div class="gn-notices-wrapper">
		<?php
		foreach ( $notices as $notice_id => $notice ) {
			$is_dismissible = ! empty( $notice['dismissible'] ) ? $notice['dismissible'] : false;
			$close_btn      = ! empty( $notice['close'] ) ? $notice['close'] : false;
			?>
			<div id="gn-notice-<?php echo esc_attr( $notice_id ); ?>" class="gn-notice gn-notice-<?php echo esc_attr( $notice['type'] ); ?><?php echo esc_attr( $is_dismissible ? ' gn-dismissible' : '' ); ?>">
				<?php if ( $close_btn ) { ?>
					<button class="gn-notice-dismiss-toggle gn-notice-dismissed"<?php echo $is_dismissible ? ' data-gn-notice-id="' . esc_attr( $notice_id ) . '"' : ''; ?>>
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				<?php } ?>
				<div class="gn-notice-content">
					<?php if ( ! empty( $notice['icon'] ) ) { ?>
						<div class="gn-notice-icon">
							<i class="<?php echo esc_attr( $notice['icon'] ); ?>" aria-hidden="true"></i>
						</div>
					<?php } ?>
					<?php if ( ! empty( $notice['title'] ) || ! empty( $notice['description'] ) ) { ?>
						<div class="gn-notice-info">
							<?php if ( ! empty( $notice['title'] ) ) { ?>
								<h3 class="gn-title"><?php echo esc_html( $notice['title'] ); ?></h3>
							<?php } ?>
							<?php if ( ! empty( $notice['description'] ) ) { ?>
								<p class="gn-description"><?php echo wp_kses_post( $notice['description'] ); ?></p>
							<?php } ?>
						</div>
					<?php } ?>
				</div>
				<?php if ( ! empty( $notice['primary_button']['url'] ) || ! empty( $notice['secondary_button']['url'] ) ) { ?>
					<div class="gn-notice-actions">
						<?php
						foreach ( array( 'secondary_button', 'primary_button' ) as $button_key ) {
							$button = $notice[ $button_key ];
							if ( empty( $button['url'] ) || empty( $button['label'] ) ) {
								continue;
							}

							// Build merged attribute string
							$button_attr = '';

							// ID
							if ( ! empty( $button['id'] ) ) {
								$button_attr .= ' id="' . esc_attr( $button['id'] ) . '"';
							}

							// Class
                            $default_class = 'gn-btn gn-btn-md';
                            $custom_class  = ! empty( $button['class'] ) ? ' ' . $button['class'] : 'gn-btn-' . ( 'primary_button' === $button_key ? 'primary' : 'grey-outline' );
                            $button_attr   .= ' class="' . esc_attr( $default_class . $custom_class ) . '"';

							// Target
							if ( ! empty( $button['target'] ) ) {
								$button_attr .= ' target="' . esc_attr( $button['target'] ) . '"';
							}

							// Custom attributes
							if ( ! empty( $button['attr'] ) && is_array( $button['attr'] ) ) {
								foreach ( $button['attr'] as $key => $val ) {
									$button_attr .= ' ' . esc_attr( $key ) . '="' . esc_attr( $val ) . '"';
								}
							}
							?>
							<a href="<?php echo esc_url( $button['url'] ); ?>"<?php echo $button_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
								<?php if ( ! empty( $button['icon'] ) ) { ?>
									<i class="<?php echo esc_attr( $button['icon'] ); ?>" aria-hidden="true"></i>
								<?php } ?>
								<?php echo esc_html( $button['label'] ); ?>
							</a>
						<?php } ?>
					</div>
				<?php } ?>
			</div>
		<?php } ?>
	</div>
<?php } ?>