<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Templates use local variables supplied by the render context.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$attention = isset( $attention ) && is_array( $attention ) ? $attention : array();
$total     = isset( $attention['total'] ) ? max( 0, (int) $attention['total'] ) : 0;
$sections  = isset( $attention['sections'] ) && is_array( $attention['sections'] ) ? $attention['sections'] : array();

$priority_labels = array(
    'critical' => __( 'Critical', 'nexdirectory' ),
    'high'     => __( 'High priority', 'nexdirectory' ),
    'medium'   => __( 'Medium priority', 'nexdirectory' ),
    'low'      => __( 'Low priority', 'nexdirectory' ),
);

$normalize_item = static function ( $item ) {
    $row = array(
        'title'  => '',
        'meta'   => '',
        'url'    => '',
        'status' => '',
    );

    if ( is_scalar( $item ) ) {
        $row['title'] = sanitize_text_field( (string) $item );

        return $row;
    }

    if ( ! is_array( $item ) ) {
        return $row;
    }

    foreach ( array( 'title', 'meta', 'url', 'status' ) as $field ) {
        if ( isset( $item[ $field ] ) && is_scalar( $item[ $field ] ) && (string) $item[ $field ] !== '' ) {
            $row[ $field ] = $field === 'url' ? (string) $item[ $field ] : sanitize_text_field( (string) $item[ $field ] );
        }
    }

    return $row;
};
?>
<div class="wrap nexdirectory-admin-premium nexdirectory-operations-needs-attention">
    <?php $this->render_admin_premium_shell_styles(); ?>

    <section class="nexdirectory-admin-hero" aria-labelledby="nexdirectory-needs-attention-title">
        <span class="nexdirectory-admin-hero-kicker"><?php esc_html_e( 'Directory Operations', 'nexdirectory' ); ?></span>
        <h1 id="nexdirectory-needs-attention-title"><?php esc_html_e( 'Needs Attention', 'nexdirectory' ); ?></h1>
        <?php if ( $total > 0 ) : ?>
            <p>
                <?php
                /* translators: %d: Number of operational items requiring review. */
                echo esc_html( sprintf( _n( '%d item needs review before your directory is fully up to date.', '%d items need review before your directory is fully up to date.', $total, 'nexdirectory' ), $total ) );
                ?>
            </p>
        <?php else : ?>
            <p><?php esc_html_e( 'Everything is up to date. There are no operational items waiting for review.', 'nexdirectory' ); ?></p>
        <?php endif; ?>
    </section>

    <?php
    /**
     * Licensed add-ons can place automation controls beside the Free summary.
     * Free intentionally renders nothing at this extension point.
     */
    do_action( 'nexdirectory_needs_attention_after_hero', $attention );
    ?>

    <?php if ( $total === 0 ) : ?>
        <section class="nexdirectory-admin-panel" aria-labelledby="nexdirectory-all-clear-title">
            <h2 id="nexdirectory-all-clear-title"><span class="dashicons dashicons-yes-alt" aria-hidden="true"></span><?php esc_html_e( 'All clear', 'nexdirectory' ); ?></h2>
            <p><?php esc_html_e( 'No action is needed right now. New moderation, connection, and health checks will appear here when they need your attention.', 'nexdirectory' ); ?></p>
        </section>
    <?php else : ?>
        <section class="nexdirectory-admin-panel" aria-labelledby="nexdirectory-attention-summary-title">
            <h2 id="nexdirectory-attention-summary-title"><span class="dashicons dashicons-warning" aria-hidden="true"></span><?php esc_html_e( 'Review queue', 'nexdirectory' ); ?></h2>
            <div class="nexdirectory-admin-grid">
                <div class="nexdirectory-admin-stat-card">
                    <p class="nexdirectory-admin-stat-label"><?php esc_html_e( 'Items requiring attention', 'nexdirectory' ); ?></p>
                    <p class="nexdirectory-admin-stat-value"><?php echo esc_html( (string) $total ); ?></p>
                </div>
                <div class="nexdirectory-admin-stat-card">
                    <p class="nexdirectory-admin-stat-label"><?php esc_html_e( 'Active sections', 'nexdirectory' ); ?></p>
                    <p class="nexdirectory-admin-stat-value"><?php echo esc_html( (string) count( $sections ) ); ?></p>
                </div>
            </div>
        </section>

        <?php foreach ( $sections as $section ) : ?>
            <?php
            if ( ! is_array( $section ) ) {
                continue;
            }

            $count       = isset( $section['count'] ) ? max( 0, (int) $section['count'] ) : 0;
            $label       = isset( $section['label'] ) ? sanitize_text_field( (string) $section['label'] ) : __( 'Operational review', 'nexdirectory' );
            $description = isset( $section['description'] ) ? sanitize_text_field( (string) $section['description'] ) : '';
            $url         = isset( $section['url'] ) ? (string) $section['url'] : '';
            $cta         = isset( $section['cta'] ) ? sanitize_text_field( (string) $section['cta'] ) : __( 'Review items', 'nexdirectory' );
            $priority    = isset( $section['priority'] ) ? strtolower( sanitize_key( (string) $section['priority'] ) ) : '';
            $items       = isset( $section['items'] ) && is_array( $section['items'] ) ? $section['items'] : array();
            $priority    = isset( $priority_labels[ $priority ] ) ? $priority : '';
            $item_labels = array();

            foreach ( $items as $item ) {
                $item_label = $normalize_item( $item );

                if ( $item_label['title'] !== '' ) {
                    $item_labels[] = $item_label;
                }
            }

            if ( $count === 0 && empty( $item_labels ) ) {
                continue;
            }
            ?>
            <section class="nexdirectory-admin-panel" aria-label="<?php echo esc_attr( $label ); ?>">
                <div class="nexdirectory-admin-toolbar">
                    <h2><span class="dashicons dashicons-flag" aria-hidden="true"></span><?php echo esc_html( $label ); ?></h2>
                    <span class="nexdirectory-admin-stat-label">
                        <?php
                        /* translators: %d: Number of items in this operational section. */
                        echo esc_html( sprintf( _n( '%d item', '%d items', $count, 'nexdirectory' ), $count ) );
                        ?>
                    </span>
                    <?php if ( $priority !== '' ) : ?>
                        <span class="nexdirectory-admin-stat-label"><?php echo esc_html( $priority_labels[ $priority ] ); ?></span>
                    <?php endif; ?>
                </div>

                <?php if ( $description !== '' ) : ?>
                    <p><?php echo esc_html( $description ); ?></p>
                <?php endif; ?>

                <?php if ( ! empty( $item_labels ) ) : ?>
                    <div class="nexdirectory-admin-table-wrap">
                        <table class="widefat striped">
                            <thead>
                                <tr>
                                    <th><?php esc_html_e( 'Item', 'nexdirectory' ); ?></th>
                                    <th><?php esc_html_e( 'Details', 'nexdirectory' ); ?></th>
                                    <th><?php esc_html_e( 'Status', 'nexdirectory' ); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ( $item_labels as $item_label ) : ?>
                                    <tr>
                                        <td>
                                            <?php if ( $item_label['url'] !== '' ) : ?>
                                                <a href="<?php echo esc_url( $item_label['url'] ); ?>"><?php echo esc_html( $item_label['title'] ); ?></a>
                                            <?php else : ?>
                                                <?php echo esc_html( $item_label['title'] ); ?>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo esc_html( $item_label['meta'] !== '' ? $item_label['meta'] : '—' ); ?></td>
                                        <td><?php echo esc_html( $item_label['status'] !== '' ? $item_label['status'] : '—' ); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>

                <?php if ( $url !== '' ) : ?>
                    <p><a class="button button-secondary" href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $cta ); ?></a></p>
                <?php endif; ?>
            </section>
        <?php endforeach; ?>
    <?php endif; ?>
</div>
