<?php
/**
 * Template Name: HVAC Order Summary
 * Template for displaying single HVAC Order Summary.
 *
 * This template displays order and attendee details for a specific order,
 * following the Astra theme structure and plugin architecture.
 * Design Reference: docs/REQUIREMENTS.md, implementation_plan.md
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Ensure the data class is available
if ( ! class_exists( 'HVAC_Order_Summary_Data' ) ) {
    $class_path = plugin_dir_path( __FILE__ ) . '../includes/community/class-order-summary-data.php';
    if ( file_exists( $class_path ) ) {
        require_once $class_path;
    } else {
        echo "<p>Error: Order Summary data handler not found.</p>";
        return;
    }
}

// Check if user is logged in
if ( ! is_user_logged_in() ) {
    get_header();
    echo '<div id="primary" class="content-area primary ast-container">';
    echo '<main id="main" class="site-main">';
    echo '<div class="hvac-login-required" style="padding: 30px; background-color: #f8f9fa; border: 1px solid #e9ecef; border-radius: 8px; text-align: center; margin: 30px 0;">';
    echo '<h2>Authentication Required</h2>';
    echo '<p>Please log in to view the order summary.</p>';
    echo '<p><a href="' . esc_url(home_url('/community-login/')) . '" class="ast-button ast-button-primary">Log In</a></p>';
    echo '</div>';
    echo '</main></div>';
    get_footer();
    exit;
}

// Get order ID from query var or context
$order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0;

if ( $order_id <= 0 ) {
    get_header();
    echo '<div id="primary" class="content-area primary ast-container">';
    echo '<main id="main" class="site-main">';
    echo '<div class="hvac-error">No order specified. Please return to your dashboard.</div>';
    echo '<p><a href="' . esc_url(home_url('/hvac-dashboard/')) . '" class="ast-button ast-button-primary">Return to Dashboard</a></p>';
    echo '</main></div>';
    get_footer();
    exit;
}

// Initialize order data handler
$order_summary = new HVAC_Order_Summary_Data( $order_id );

// Check if order is valid and user has permission to view it
if ( ! $order_summary->is_valid_order() || ! $order_summary->user_can_view_order() ) {
    get_header();
    echo '<div id="primary" class="content-area primary ast-container">';
    echo '<main id="main" class="site-main">';
    echo '<div class="hvac-error">Order not found or you do not have permission to view this order.</div>';
    echo '<p><a href="' . esc_url(home_url('/hvac-dashboard/')) . '" class="ast-button ast-button-primary">Return to Dashboard</a></p>';
    echo '</main></div>';
    get_footer();
    exit;
}

// Get order data
$order_details = $order_summary->get_order_details();
$tickets = $order_details['tickets'];
$events = $order_details['events'];
$notes = $order_summary->get_order_notes();

get_header();
?>
<div id="primary" class="content-area primary ast-container">
    <main id="main" class="site-main">
        
        <!-- Header & Navigation -->
        <div class="hvac-dashboard-header">
            <h1 class="entry-title">Order Summary: #<?php echo esc_html( $order_details['order_number'] ); ?></h1>
            <div class="hvac-dashboard-nav">
                <?php if(!empty($events) && isset($events[0]['id'])): ?>
                    <a href="<?php echo esc_url(add_query_arg('event_id', $events[0]['id'], home_url('/event-summary/'))); ?>" class="ast-button ast-button-secondary">Back to Event Summary</a>
                <?php endif; ?>
                <a href="<?php echo esc_url(home_url('/hvac-dashboard/')); ?>" class="ast-button ast-button-primary">Dashboard</a>
                <?php
                // Email attendees link (Phase 2 feature)
                if (count($tickets) > 0) {
                    echo '<a href="#" class="ast-button ast-button-secondary">Email Attendees</a>';
                }
                ?>
            </div>
        </div>
        
        <!-- Order Overview Section -->
        <section class="hvac-summary-section">
            <h2>Order Overview</h2>
            <div class="hvac-summary-content">
                <!-- Order Details -->
                <div class="hvac-details-card">
                    <table class="hvac-details-table">
                        <tr>
                            <th>Order Number:</th>
                            <td><?php echo esc_html( $order_details['order_number'] ); ?></td>
                        </tr>
                        <tr>
                            <th>Purchaser:</th>
                            <td>
                                <?php echo esc_html( $order_details['purchaser_name'] ); ?>
                                <?php if (!empty($order_details['purchaser_email'])) : ?>
                                    <div class="hvac-detail-subtext"><?php echo esc_html( $order_details['purchaser_email'] ); ?></div>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php if (!empty($order_details['organization'])) : ?>
                        <tr>
                            <th>Organization:</th>
                            <td><?php echo esc_html( $order_details['organization'] ); ?></td>
                        </tr>
                        <?php endif; ?>
                        <tr>
                            <th>Purchase Date:</th>
                            <td><?php echo esc_html( $order_details['purchase_date'] ); ?></td>
                        </tr>
                        <tr>
                            <th>Status:</th>
                            <td><?php echo esc_html( ucfirst( $order_details['status'] ) ); ?></td>
                        </tr>
                        <tr>
                            <th>Total Price:</th>
                            <td><?php echo esc_html( $order_details['total_price'] ); ?></td>
                        </tr>
                        <?php if (!empty($order_details['payment_method'])) : ?>
                        <tr>
                            <th>Payment Method:</th>
                            <td><?php echo esc_html( $order_details['payment_method'] ); ?></td>
                        </tr>
                        <?php endif; ?>
                        <?php if (!empty($order_details['billing_address'])) : ?>
                        <tr>
                            <th>Billing Address:</th>
                            <td><?php echo esc_html( $order_details['billing_address'] ); ?></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                </div>
            </div>
        </section>
        
        <!-- Events Section -->
        <?php if (!empty($events)) : ?>
        <section class="hvac-summary-section">
            <h2>Events</h2>
            <div class="hvac-summary-content">
                <div class="hvac-events-list">
                    <?php foreach ($events as $event) : ?>
                    <div class="hvac-event-card">
                        <h3>
                            <a href="<?php echo esc_url(add_query_arg('event_id', $event['id'], home_url('/event-summary/'))); ?>">
                                <?php echo esc_html($event['title']); ?>
                            </a>
                        </h3>
                        <?php if (!empty($event['start_date'])) : ?>
                        <div class="hvac-event-date">
                            <?php echo esc_html($event['start_date']); ?>
                            <?php if (!empty($event['end_date']) && $event['start_date'] !== $event['end_date']) : ?>
                                - <?php echo esc_html($event['end_date']); ?>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                        <?php if (!empty($event['venue'])) : ?>
                        <div class="hvac-event-venue">
                            <i class="fas fa-map-marker-alt"></i> <?php echo esc_html($event['venue']); ?>
                        </div>
                        <?php endif; ?>
                        <div class="hvac-event-links">
                            <a href="<?php echo esc_url($event['permalink']); ?>" target="_blank" class="hvac-view-link">View Public Page</a>
                            <a href="<?php echo esc_url(add_query_arg('event_id', $event['id'], home_url('/event-summary/'))); ?>" class="hvac-summary-link">Event Summary</a>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php endif; ?>
        
        <!-- Tickets & Attendees Section -->
        <section class="hvac-summary-section">
            <h2>Tickets & Attendees</h2>
            <div class="hvac-summary-content">
                <?php if (!empty($tickets)) : ?>
                <table class="hvac-tickets-table">
                    <thead>
                        <tr>
                            <th>Attendee</th>
                            <th>Email</th>
                            <th>Ticket Type</th>
                            <th>Event</th>
                            <th>Price</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($tickets as $ticket) : ?>
                        <tr>
                            <td><?php echo esc_html($ticket['attendee_name']); ?></td>
                            <td><?php echo esc_html($ticket['attendee_email']); ?></td>
                            <td><?php echo esc_html($ticket['ticket_type']); ?></td>
                            <td>
                                <?php if (!empty($ticket['event_id'])) : ?>
                                <a href="<?php echo esc_url(add_query_arg('event_id', $ticket['event_id'], home_url('/event-summary/'))); ?>">
                                    <?php echo esc_html($ticket['event_title']); ?>
                                </a>
                                <?php else : ?>
                                <?php echo esc_html($ticket['event_title'] ?? 'N/A'); ?>
                                <?php endif; ?>
                            </td>
                            <td><?php echo !empty($ticket['price']) ? '$' . number_format((float)$ticket['price'], 2) : 'N/A'; ?></td>
                            <td><?php echo $ticket['checked_in'] ? 'Checked In' : 'Not Checked In'; ?></td>
                        </tr>
                        <?php if (!empty($ticket['additional_fields'])) : ?>
                        <tr class="attendee-additional-fields">
                            <td colspan="6">
                                <details>
                                    <summary>Additional Information</summary>
                                    <div class="additional-fields-content">
                                        <table class="additional-fields-table">
                                            <?php foreach ($ticket['additional_fields'] as $field) : ?>
                                            <tr>
                                                <th><?php echo esc_html($field['label']); ?>:</th>
                                                <td><?php echo esc_html($field['value']); ?></td>
                                            </tr>
                                            <?php endforeach; ?>
                                        </table>
                                    </div>
                                </details>
                            </td>
                        </tr>
                        <?php endif; ?>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else : ?>
                <p>No tickets or attendees found for this order.</p>
                <?php endif; ?>
            </div>
        </section>
        
        <!-- Order Notes Section -->
        <?php if (!empty($notes)) : ?>
        <section class="hvac-summary-section">
            <h2>Order Notes</h2>
            <div class="hvac-summary-content">
                <div class="hvac-notes-list">
                    <?php foreach ($notes as $note) : ?>
                    <div class="hvac-note-item">
                        <div class="hvac-note-content"><?php echo wp_kses_post($note['content']); ?></div>
                        <div class="hvac-note-meta">
                            <span class="hvac-note-date"><?php echo esc_html($note['date']); ?></span>
                            <?php if (!empty($note['author'])) : ?>
                            <span class="hvac-note-author">by <?php echo esc_html($note['author']); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php endif; ?>
        
    </main>
</div>

<style>
    /* Order Summary Styles */
    .hvac-dashboard-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
    }
    
    .hvac-dashboard-nav {
        display: flex;
        gap: 10px;
    }
    
    .hvac-summary-section {
        margin-bottom: 40px;
        background: #f8f9fa;
        border-radius: 8px;
        padding: 20px;
        border: 1px solid #e9ecef;
    }
    
    .hvac-summary-section h2 {
        margin-top: 0;
        margin-bottom: 20px;
        border-bottom: 1px solid #eee;
        padding-bottom: 10px;
    }
    
    .hvac-summary-content {
        margin-top: 10px;
    }
    
    /* Order Details Table */
    .hvac-details-table {
        width: 100%;
        border-collapse: collapse;
    }
    
    .hvac-details-table th,
    .hvac-details-table td {
        padding: 10px;
        text-align: left;
        border-bottom: 1px solid #eee;
    }
    
    .hvac-details-table th {
        width: 150px;
        font-weight: bold;
        vertical-align: top;
    }
    
    .hvac-detail-subtext {
        font-size: 0.9em;
        color: #666;
        margin-top: 5px;
    }
    
    /* Events List */
    .hvac-events-list {
        display: flex;
        flex-wrap: wrap;
        gap: 20px;
    }
    
    .hvac-event-card {
        flex: 1;
        min-width: 250px;
        background: #fff;
        border: 1px solid #e9ecef;
        border-radius: 5px;
        padding: 15px;
    }
    
    .hvac-event-card h3 {
        margin-top: 0;
        margin-bottom: 10px;
    }
    
    .hvac-event-date,
    .hvac-event-venue {
        margin-bottom: 10px;
        color: #666;
        font-size: 0.9em;
    }
    
    .hvac-event-links {
        display: flex;
        justify-content: space-between;
        margin-top: 15px;
    }
    
    .hvac-event-links a {
        display: inline-block;
        font-size: 0.9em;
        text-decoration: none;
    }
    
    /* Tickets Table */
    .hvac-tickets-table {
        width: 100%;
        border-collapse: collapse;
    }
    
    .hvac-tickets-table th,
    .hvac-tickets-table td {
        padding: 12px;
        text-align: left;
        border-bottom: 1px solid #eee;
    }
    
    .hvac-tickets-table th {
        background-color: #f1f1f1;
        font-weight: bold;
    }
    
    .hvac-tickets-table tr:nth-child(even) {
        background-color: #f8f8f8;
    }
    
    .hvac-tickets-table tr:hover {
        background-color: #f0f0f0;
    }
    
    .attendee-additional-fields {
        background-color: #f5f5f5;
    }
    
    .attendee-additional-fields td {
        padding: 0;
    }
    
    .attendee-additional-fields details {
        padding: 10px;
    }
    
    .attendee-additional-fields summary {
        cursor: pointer;
        padding: 5px;
        color: #0073aa;
    }
    
    .additional-fields-content {
        padding: 10px;
        background: #fff;
        border-radius: 5px;
        margin-top: 10px;
    }
    
    .additional-fields-table {
        width: 100%;
        border-collapse: collapse;
    }
    
    .additional-fields-table th,
    .additional-fields-table td {
        padding: 5px;
        text-align: left;
        border-bottom: 1px solid #eee;
    }
    
    .additional-fields-table th {
        width: 150px;
        font-weight: normal;
        color: #666;
    }
    
    /* Order Notes */
    .hvac-notes-list {
        border: 1px solid #e0e0e0;
        border-radius: 5px;
        background: #fff;
    }
    
    .hvac-note-item {
        padding: 15px;
        border-bottom: 1px solid #e0e0e0;
    }
    
    .hvac-note-item:last-child {
        border-bottom: none;
    }
    
    .hvac-note-content {
        margin-bottom: 5px;
    }
    
    .hvac-note-meta {
        font-size: 0.85em;
        color: #777;
    }
    
    .hvac-note-date {
        margin-right: 10px;
    }
    
    /* Responsive adjustments */
    @media (max-width: 768px) {
        .hvac-dashboard-header {
            flex-direction: column;
            align-items: flex-start;
        }
        
        .hvac-dashboard-nav {
            margin-top: 15px;
            flex-wrap: wrap;
        }
        
        .hvac-dashboard-nav a {
            margin-bottom: 5px;
        }
        
        .hvac-details-table th {
            width: 120px;
        }
        
        .hvac-tickets-table {
            display: block;
            overflow-x: auto;
        }
        
        .hvac-event-card {
            flex: 1 0 100%;
        }
    }
</style>

<?php get_footer(); ?>