<?php
// Ensure the parent class is available
if ( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

/**
 * Creates the Visitors (Attendees) List Table.
 * This class replaces the old visitors.php file.
 */
class Event_Monster_Free_Visitors_List_Table extends WP_List_Table {

    /**
     * Constructor.
     */
    public function __construct() {
        parent::__construct( [
            'singular' => __( 'Visitor', 'event-monster-premium' ),
            'plural'   => __( 'Visitors', 'event-monster-premium' ),
            'ajax'     => false
        ] );
    }

    /**
     * Prepare the items for the table.
     */
    public function prepare_items() {
        $this->_column_headers = array($this->get_columns(), $this->get_hidden_columns(), $this->get_sortable_columns());
        $this->items = $this->get_visitors_data();
    }

    /**
     * Define the columns for the table.
     * **FIXED:** The keys now correctly match your database columns.
     */
    public function get_columns() {
        return array(
            'cb'         => '<input type="checkbox" />',
            'name'       => __( 'Name', 'event-monster-premium' ),
            'email'      => __( 'Email', 'event-monster-premium' ),
            'phone'      => __( 'Phone', 'event-monster-premium' ),
            'event_id'   => __( 'Event', 'event-monster-premium' )
        );
    }

    /**
     * Define the default column rendering.
     */
    public function column_default( $item, $column_name ) {
        switch ( $column_name ) {
            case 'email':
            case 'phone':
                return $item[ $column_name ];
            case 'event_id':
                // Display the event's title instead of just the ID
                return get_the_title( $item[ $column_name ] );
            default:
                return print_r( $item, true );
        }
    }

    /**
     * Render the checkbox column.
     */
    function column_cb( $item ) {
        return sprintf(
            '<input type="checkbox" name="visitor[]" value="%s" />', $item['id']
        );
    }

    /**
     * Render the 'name' column and add row actions.
     * **FIXED:** We combine 'first_name' and 'last_name' from the data here.
     */
    public function column_name($item) {
        $delete_nonce = wp_create_nonce( 'em_delete_visitor' );
        $actions = array(
            'delete' => sprintf('<a href="?page=%s&action=delete&visitor=%s&_wpnonce=%s">Delete</a>', esc_attr($_REQUEST['page']), absint($item['id']), $delete_nonce),
        );
        
        // Combine the first and last name for display
        $full_name = $item['first_name'] . ' ' . $item['last_name'];
        
        return sprintf('%1$s %2$s', $full_name, $this->row_actions($actions));
    }

    /**
     * Get the visitor data from the database.
     */
    private function get_visitors_data() {
        global $wpdb;
        $table_name = $wpdb->prefix . 'em_attendees';

        // Select all necessary columns
        $results = $wpdb->get_results( "SELECT id, first_name, last_name, email, phone, event_id FROM `$table_name` ORDER BY id DESC", ARRAY_A );
        
        return $results;
    }
}