<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.WP.GlobalVariablesOverride.Prohibited -- False positives: locally-scoped variables (template view-vars via TemplateLoader::render() extract+include within a method, or closure locals), not plugin globals.
/**
 * Shared lane summary banner.
 *
 * @var array $lane_summary
 */

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

$lane_summary = is_array( $lane_summary ?? null ) ? $lane_summary : [];
if ( empty( $lane_summary['visible'] ) ) {
    return;
}

$state = sanitize_key( (string) ( $lane_summary['state'] ?? 'info' ) );
if ( ! in_array( $state, [ 'ok', 'warn', 'error', 'info' ], true ) ) {
    $state = 'info';
}

$title = trim( (string) ( $lane_summary['title'] ?? __( 'Lane summary', 'mybookaro' ) ) );
$summary = trim( (string) ( $lane_summary['summary'] ?? '' ) );
$state_label = trim( (string) ( $lane_summary['state_label'] ?? __( 'Info', 'mybookaro' ) ) );
$lane_label = trim( (string) ( $lane_summary['lane_label'] ?? '' ) );
$model_label = trim( (string) ( $lane_summary['model_label'] ?? __( 'Current setup model', 'mybookaro' ) ) );
$model_summary = trim( (string) ( $lane_summary['model_summary'] ?? '' ) );
$model_items = [];
$raw_model_items = is_array( $lane_summary['model_items'] ?? null ) ? $lane_summary['model_items'] : [];
foreach ( $raw_model_items as $model_item ) {
    if ( ! is_array( $model_item ) ) {
        continue;
    }
    $item_label = trim( (string) ( $model_item['label'] ?? '' ) );
    $item_value = trim( (string) ( $model_item['value'] ?? '' ) );
    if ( '' === $item_label || '' === $item_value ) {
        continue;
    }
    $model_items[] = [
        'label' => $item_label,
        'value' => $item_value,
    ];
}
$has_model_snapshot = ( '' !== $model_summary ) || ! empty( $model_items );
$top_blocker_label = trim( (string) ( $lane_summary['top_blocker_label'] ?? __( 'Top blocker', 'mybookaro' ) ) );
$top_blocker = trim( (string) ( $lane_summary['top_blocker'] ?? '' ) );
$next_action_label = trim( (string) ( $lane_summary['next_action_label'] ?? __( 'Next action', 'mybookaro' ) ) );
$next_action = is_array( $lane_summary['next_action'] ?? null ) ? $lane_summary['next_action'] : [];
$next_action_text = trim( (string) ( $next_action['detail'] ?? '' ) );
$next_action_url = trim( (string) ( $next_action['url'] ?? '' ) );
$next_action_cta = trim( (string) ( $next_action['label'] ?? '' ) );

if ( '' === $title ) {
    $title = __( 'Lane summary', 'mybookaro' );
}
if ( '' === $state_label ) {
    $state_label = __( 'Info', 'mybookaro' );
}
if ( '' === $lane_label ) {
    $lane_label = __( 'Generic Directory', 'mybookaro' );
}
if ( '' === $top_blocker ) {
    $top_blocker = __( 'No blockers detected right now.', 'mybookaro' );
}
?>
<div class="bookaro-card bookaro-lane-summary-banner">
    <div class="bookaro-card__header">
        <h2><?php echo esc_html( $title ); ?></h2>
        <?php if ( '' !== $summary ) : ?>
            <p class="description"><?php echo esc_html( $summary ); ?></p>
        <?php endif; ?>
    </div>
    <div class="bookaro-status-list">
        <div class="bookaro-status-item">
            <div class="bookaro-status-item__label"><?php esc_html_e( 'Active lane', 'mybookaro' ); ?></div>
            <div class="bookaro-status-item__value">
                <span class="bookaro-status-pill <?php echo esc_attr( 'bookaro-status-pill--' . $state ); ?>">
                    <?php echo esc_html( $lane_label ); ?>
                </span>
                <span class="bookaro-status-pill bookaro-status-pill--info"><?php echo esc_html( $state_label ); ?></span>
            </div>
        </div>
        <?php if ( $has_model_snapshot ) : ?>
            <div class="bookaro-status-item">
                <div class="bookaro-status-item__label"><?php echo esc_html( $model_label ); ?></div>
                <div class="bookaro-status-item__value">
                    <?php if ( '' !== $model_summary ) : ?>
                        <div class="bookaro-status-item__meta"><?php echo esc_html( $model_summary ); ?></div>
                    <?php endif; ?>
                    <?php foreach ( $model_items as $model_item ) : ?>
                        <div class="bookaro-status-item__meta">
                            <strong><?php echo esc_html( (string) $model_item['label'] ); ?>:</strong>
                            <?php echo esc_html( (string) $model_item['value'] ); ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
        <div class="bookaro-status-item">
            <div class="bookaro-status-item__label"><?php echo esc_html( $top_blocker_label ); ?></div>
            <div class="bookaro-status-item__value"><?php echo esc_html( $top_blocker ); ?></div>
        </div>
        <div class="bookaro-status-item">
            <div class="bookaro-status-item__label"><?php echo esc_html( $next_action_label ); ?></div>
            <div class="bookaro-status-item__value">
                <?php if ( '' !== $next_action_text ) : ?>
                    <div class="bookaro-status-item__meta"><?php echo esc_html( $next_action_text ); ?></div>
                <?php endif; ?>
                <?php if ( '' !== $next_action_url && '' !== $next_action_cta ) : ?>
                    <div class="bookaro-status-item__actions">
                        <a class="button button-small" href="<?php echo esc_url( $next_action_url ); ?>">
                            <?php echo esc_html( $next_action_cta ); ?>
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
