<?php
/**
 * Admin Bookings Management Page Template
 *
 * @since   PBOOKING_SINCE
 * @package Pulse_Booking
 *
 * @var array $bookings    Array of booking objects
 * @var array $statistics  Booking statistics
 */

// Security check
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Prefix variables passed to template
$nemtly_booking_bookings   = $bookings ?? [];
$nemtly_booking_statistics = $statistics ?? [];

// Validate and sanitize GET parameters
$nemtly_booking_current_status = 'all';
$nemtly_booking_search_term    = '';

// Validate status parameter
if ( ! empty( $_GET['status'] ) ) {
    $nemtly_booking_status         = sanitize_text_field( wp_unslash( $_GET['status'] ) );
    $nemtly_booking_valid_statuses = [ 'all', 'pending', 'confirmed', 'cancelled' ];
    if ( in_array( $nemtly_booking_status, $nemtly_booking_valid_statuses, true ) ) {
        $nemtly_booking_current_status = $nemtly_booking_status;
    }
}

// Validate search term
if ( ! empty( $_GET['s'] ) ) {
    $nemtly_booking_search_term = sanitize_text_field( wp_unslash( $_GET['s'] ) );
    // Limit search term length for security
    if ( strlen( $nemtly_booking_search_term ) > 100 ) {
        $nemtly_booking_search_term = substr( $nemtly_booking_search_term, 0, 100 );
    }
}

// Status counts for filter tabs
$nemtly_booking_status_counts = [
    'all'       => $nemtly_booking_statistics['total_bookings'] ?? 0,
    'pending'   => $nemtly_booking_statistics['pending_bookings'] ?? 0,
    'confirmed' => $nemtly_booking_statistics['confirmed_bookings'] ?? 0,
    'cancelled' => $nemtly_booking_statistics['cancelled_bookings'] ?? 0,
];

// Status labels
$nemtly_booking_status_labels = [
    'all'       => __( 'All', 'nemtly-booking' ),
    'pending'   => __( 'Pending', 'nemtly-booking' ),
    'confirmed' => __( 'Confirmed', 'nemtly-booking' ),
    'cancelled' => __( 'Cancelled', 'nemtly-booking' ),
];
?>

<div class="wrap">
    <h1 class="wp-heading-inline">
        <?php esc_html_e( 'Bookings', 'nemtly-booking' ); ?>
    </h1>

    <!-- Status Filter Tabs -->
    <div class="nb-admin-tabs">
        <h2 class="nav-tab-wrapper">
            <?php foreach ( $nemtly_booking_status_labels as $nemtly_booking_status => $nemtly_booking_label ) : ?>
                <?php
                $nemtly_booking_url = add_query_arg(
                    [
						'page'   => 'nemtly-bookings',
						'status' => $nemtly_booking_status,
					],
                    admin_url( 'admin.php' )
                );

                $nemtly_booking_class = 'nav-tab';
                if ( $nemtly_booking_current_status === $nemtly_booking_status ) {
                    $nemtly_booking_class .= ' nav-tab-active';
                }
                ?>
                <a href="<?php echo esc_url( $nemtly_booking_url ); ?>" class="<?php echo esc_attr( $nemtly_booking_class ); ?>">
                    <?php echo esc_html( $nemtly_booking_label ); ?>
                    <span class="count">(<?php echo absint( $nemtly_booking_status_counts[ $nemtly_booking_status ] ); ?>)</span>
                </a>
            <?php endforeach; ?>
        </h2>
    </div>

    <!-- Search and Bulk Actions -->
    <div class="nb-admin-filters">
        <form method="GET" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
            <?php wp_nonce_field( 'nemtly_bookings_search', 'nemtly_bookings_nonce' ); ?>
            <input type="hidden" name="page" value="nemtly-bookings">
            <input type="hidden" name="status" value="<?php echo esc_attr( $nemtly_booking_current_status ); ?>">

            <div class="nb-filter-row">
                <div class="nb-search-box">
                    <input type="search" name="s" value="<?php echo esc_attr( $nemtly_booking_search_term ); ?>"
                        placeholder="<?php esc_attr_e( 'Search bookings...', 'nemtly-booking' ); ?>">
                    <button type="submit" class="button">
                        <?php esc_html_e( 'Search', 'nemtly-booking' ); ?>
                    </button>
                </div>

                <div class="nb-bulk-actions">
                    <select id="bulk-action-selector" disabled>
                        <option value=""><?php esc_html_e( 'Bulk Actions', 'nemtly-booking' ); ?></option>
                        <option value="confirm"><?php esc_html_e( 'Confirm', 'nemtly-booking' ); ?></option>
                        <option value="cancel"><?php esc_html_e( 'Cancel', 'nemtly-booking' ); ?></option>
                        <option value="delete"><?php esc_html_e( 'Delete', 'nemtly-booking' ); ?></option>
                    </select>
                    <button type="button" class="button" id="bulk-action-apply" disabled>
                        <?php esc_html_e( 'Apply', 'nemtly-booking' ); ?>
                    </button>
                </div>
            </div>
        </form>
    </div>

    <!-- Bookings Table -->
    <div class="nb-bookings-table-container">
        <?php if ( ! empty( $nemtly_booking_bookings ) ) : ?>
            <table class="wp-list-table widefat fixed striped nb-bookings-table">
                <thead>
                    <tr>
                        <td class="manage-column column-cb check-column">
                            <input type="checkbox" id="cb-select-all">
                        </td>
                        <th class="manage-column column-booking-id">
                            <?php esc_html_e( 'Booking ID', 'nemtly-booking' ); ?>
                        </th>
                        <th class="manage-column column-customer">
                            <?php esc_html_e( 'Customer', 'nemtly-booking' ); ?>
                        </th>
                        <th class="manage-column column-event">
                            <?php esc_html_e( 'Event', 'nemtly-booking' ); ?>
                        </th>
                        <th class="manage-column column-booking-date">
                            <?php esc_html_e( 'Booking Date', 'nemtly-booking' ); ?>
                        </th>
                        <th class="manage-column column-status">
                            <?php esc_html_e( 'Status', 'nemtly-booking' ); ?>
                        </th>
                        <th class="manage-column column-created">
                            <?php esc_html_e( 'Created', 'nemtly-booking' ); ?>
                        </th>
                        <th class="manage-column column-actions">
                            <?php esc_html_e( 'Actions', 'nemtly-booking' ); ?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $nemtly_booking_bookings as $nemtly_booking_booking ) : ?>
                        <?php
                        // Get event details for this booking
                        $nemtly_booking_event       = get_post( $nemtly_booking_booking->get_event_id() );
                        $nemtly_booking_event_title = $nemtly_booking_event ? $nemtly_booking_event->post_title : __( 'Unknown Event', 'nemtly-booking' );

                        // Format dates
                        $nemtly_booking_date         = $nemtly_booking_booking->get_booking_date();
                        $nemtly_booking_created_date = $nemtly_booking_booking->get_created_at();

                        // Status badge class
                        $nemtly_booking_status_class = 'nb-status-' . $nemtly_booking_booking->get_booking_status();
                        ?>
                        <tr data-booking-id="<?php echo esc_attr( $nemtly_booking_booking->get_id() ); ?>">
                            <th class="check-column">
                                <input type="checkbox" name="booking_ids[]"
                                    value="<?php echo esc_attr( $nemtly_booking_booking->get_id() ); ?>" class="booking-checkbox">
                            </th>
                            <td class="column-booking-id">
                                <strong>#<?php echo esc_html( $nemtly_booking_booking->get_booking_uuid() ); ?></strong>
                            </td>
                            <td class="column-customer">
                                <div class="nb-customer-info">
                                    <strong><?php echo esc_html( $nemtly_booking_booking->get_attendee_name() ); ?></strong><br>
                                    <a href="mailto:<?php echo esc_attr( $nemtly_booking_booking->get_attendee_email() ); ?>">
                                        <?php echo esc_html( $nemtly_booking_booking->get_attendee_email() ); ?>
                                    </a>
                                    <?php if ( $nemtly_booking_booking->get_attendee_phone() ) : ?>
                                        <br><small><?php echo esc_html( $nemtly_booking_booking->get_attendee_phone() ); ?></small>
                                    <?php endif; ?>
                                </div>
                            </td>
                            <td class="column-event">
                                <?php if ( $nemtly_booking_event ) : ?>
                                    <a href="<?php echo esc_url( get_edit_post_link( $nemtly_booking_event->ID ) ); ?>">
                                        <?php echo esc_html( $nemtly_booking_event_title ); ?>
                                    </a>
                                <?php else : ?>
                                    <span class="nb-deleted-event"><?php echo esc_html( $nemtly_booking_event_title ); ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="column-booking-date">
                                <?php echo esc_html( $nemtly_booking_date ); ?>
                            </td>
                            <td class="column-status">
                                <span class="nb-status-badge <?php echo esc_attr( $nemtly_booking_status_class ); ?>">
                                    <?php echo esc_html( ucfirst( $nemtly_booking_booking->get_booking_status() ) ); ?>
                                </span>
                            </td>
                            <td class="column-created">
                                <?php echo esc_html( gmdate( 'Y-m-d H:i', strtotime( $nemtly_booking_created_date ) ) ); ?>
                            </td>
                            <td class="column-actions">
                                <div class="nb-booking-actions">
                                    <?php if ( $nemtly_booking_booking->is_pending() ) : ?>
                                        <button type="button" class="button button-small nb-action-confirm"
                                            data-booking-id="<?php echo esc_attr( $nemtly_booking_booking->get_id() ); ?>"
                                            title="<?php esc_attr_e( 'Confirm booking', 'nemtly-booking' ); ?>">
                                            <?php esc_html_e( 'Confirm', 'nemtly-booking' ); ?>
                                        </button>
                                    <?php endif; ?>

                                    <?php if ( ! $nemtly_booking_booking->is_cancelled() ) : ?>
                                        <button type="button" class="button button-small nb-action-cancel"
                                            data-booking-id="<?php echo esc_attr( $nemtly_booking_booking->get_id() ); ?>"
                                            title="<?php esc_attr_e( 'Cancel booking', 'nemtly-booking' ); ?>">
                                            <?php esc_html_e( 'Cancel', 'nemtly-booking' ); ?>
                                        </button>
                                    <?php endif; ?>

                                    <button type="button" class="button button-small button-link-delete nb-action-delete"
                                        data-booking-id="<?php echo esc_attr( $nemtly_booking_booking->get_id() ); ?>"
                                        title="<?php esc_attr_e( 'Delete booking', 'nemtly-booking' ); ?>">
                                        <?php esc_html_e( 'Delete', 'nemtly-booking' ); ?>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

            <!-- Pagination would go here -->
            <div class="nb-pagination">
                <!-- TODO: Implement pagination -->
            </div>

        <?php else : ?>
            <div class="nb-no-bookings">
                <div class="nb-empty-state">
                    <div class="nb-empty-icon">
                        <span class="dashicons dashicons-calendar-alt"></span>
                    </div>
                    <h3><?php esc_html_e( 'No bookings found', 'nemtly-booking' ); ?></h3>
                    <p>
                        <?php if ( 'all' === $nemtly_booking_current_status ) : ?>
                            <?php esc_html_e( 'No bookings have been made yet.', 'nemtly-booking' ); ?>
                        <?php else : ?>
                            <?php
                            printf(
                                /* translators: %s: status name */
                                esc_html__( 'No %s bookings found.', 'nemtly-booking' ),
                                esc_html( $nemtly_booking_status_labels[ $nemtly_booking_current_status ] )
                            );
                            ?>
                        <?php endif; ?>
                    </p>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <!-- Loading overlay -->
    <div id="nb-loading-overlay" style="display: none;">
        <div class="nb-loading-spinner">
            <span class="spinner is-active"></span>
            <p><?php esc_html_e( 'Processing...', 'nemtly-booking' ); ?></p>
        </div>
    </div>
</div>

