<?php

use Yoast\WPTestUtils\WPIntegration\TestCase;

/**
 * Class Test_Event_Summary_Integration
 *
 * Integration tests for Event Summary functionality, especially involving dependencies like Event Tickets.
 */
class Test_Event_Summary_Integration extends TestCase {

    /**
     * Set up the test environment.
     */
    public function set_up(): void {
        parent::set_up();
        // Ensure the class under test is available
        require_once dirname(__DIR__, 2) . '/wp-content/plugins/hvac-community-events/includes/community/class-event-summary-data.php';

        // Activate dependent plugins using WP-CLI for better hook triggering
        // Note: This assumes WP-CLI is available in the container's PATH as 'wp'
        // And that shell_exec is permitted. Check php.ini disable_functions if it fails.
        $tec_slug = 'the-events-calendar';
        $et_slug = 'event-tickets';

        // Check if plugins are already active to avoid errors/warnings
        $tec_active = shell_exec( 'wp plugin is-active ' . escapeshellarg($tec_slug) . ' --allow-root' );
        $et_active = shell_exec( 'wp plugin is-active ' . escapeshellarg($et_slug) . ' --allow-root' );

        if ( strpos( $tec_active, 'Success' ) === false ) {
            shell_exec( 'wp plugin activate ' . escapeshellarg($tec_slug) . ' --allow-root' );
        }
         if ( strpos( $et_active, 'Success' ) === false ) {
            shell_exec( 'wp plugin activate ' . escapeshellarg($et_slug) . ' --allow-root' );
        }

        // Flush cache after activating plugins
        wp_cache_flush();
    }

    /**
     * Tear down the test environment.
     */
    public function tear_down(): void {
        parent::tear_down();
    }

    /**
     * Test event details retrieval and display.
     * @test
     */
    public function test_event_details_retrieval() {
        // Create a test event
        $event_id = $this->factory->post->create([
            'post_type' => Tribe__Events__Main::POSTTYPE,
            'post_title' => 'Test HVAC Training Event',
            'post_status' => 'publish',
            'meta_input' => [
                '_EventStartDate' => date('Y-m-d H:i:s', strtotime('+1 week')),
                '_EventEndDate' => date('Y-m-d H:i:s', strtotime('+1 week +2 hours')),
                '_EventVenue' => 'Test Venue',
                '_EventCost' => '199.99'
            ]
        ]);

        // Get event summary data
        $summary = new HVAC_Event_Summary_Data($event_id);
        $event_data = $summary->get_event_details();

        // Verify event details
        $this->assertEquals('Test HVAC Training Event', $event_data['title']);
        $this->assertEquals('Test Venue', $event_data['venue']);
        $this->assertEquals('199.99', $event_data['cost']);
        $this->assertArrayHasKey('start_date', $event_data);
        $this->assertArrayHasKey('end_date', $event_data);
    }

    /**
     * Test transaction data handling with mocked Event Tickets data.
     * @test
     */
    public function test_transaction_data_handling() {
        // Create test event
        $event_id = $this->factory->post->create([
            'post_type' => Tribe__Events__Main::POSTTYPE,
            'post_title' => 'Transaction Test Event'
        ]);

        // Mock transaction data since we can't rely on Event Tickets in integration tests
        add_filter('tribe_tickets_get_attendees', function($attendees, $post_id) use ($event_id) {
            if ($post_id === $event_id) {
                return [
                    [
                        'order_id' => '123',
                        'purchaser_name' => 'John Doe',
                        'purchase_time' => date('Y-m-d H:i:s'),
                        'ticket_name' => 'General Admission',
                        'price' => 50.00
                    ]
                ];
            }
            return $attendees;
        }, 10, 2);

        // Get transaction summary
        $summary = new HVAC_Event_Summary_Data($event_id);
        $transactions = $summary->get_transaction_summary();

        // Verify transaction data
        $this->assertIsArray($transactions);
        $this->assertCount(1, $transactions);
        $this->assertEquals('John Doe', $transactions[0]['purchaser_name']);
        $this->assertEquals(50.00, $transactions[0]['price']);
    }

    /**
     * Test breadcrumb navigation integration.
     * @test
     */
    public function test_breadcrumb_navigation() {
        // Create test event
        $event_id = $this->factory->post->create([
            'post_type' => Tribe__Events__Main::POSTTYPE,
            'post_title' => 'Breadcrumb Test Event'
        ]);

        // Get breadcrumb data
        $summary = new HVAC_Event_Summary_Data($event_id);
        $breadcrumbs = $summary->get_breadcrumb_data();

        // Verify breadcrumb structure
        $this->assertIsArray($breadcrumbs);
        $this->assertArrayHasKey('home', $breadcrumbs);
        $this->assertArrayHasKey('events', $breadcrumbs);
        $this->assertArrayHasKey('current', $breadcrumbs);
        $this->assertEquals('Breadcrumb Test Event', $breadcrumbs['current']['title']);
    }

    /**
     * Test theme styling integration for event summary.
     * @test
     */
    public function test_theme_styling_integration() {
        // Create test event
        $event_id = $this->factory->post->create([
            'post_type' => Tribe__Events__Main::POSTTYPE,
            'post_title' => 'Styling Test Event'
        ]);

        // Get summary display HTML
        $summary = new HVAC_Event_Summary_Data($event_id);
        ob_start();
        $summary->display_event_summary();
        $output = ob_get_clean();

        // Verify theme-specific classes and structure
        $this->assertStringContainsString('hvac-event-summary', $output);
        $this->assertStringContainsString('hvac-theme-compatible', $output);
        $this->assertStringContainsString('event-details-section', $output);
        $this->assertStringContainsString('transaction-summary-section', $output);
    }
}