<?php
/**
 * Tests for basic database operations without full plugin loading
 */

// Ensure we're running in test mode
if (!defined('HVAC_TEST_DIR')) {
    require_once dirname(__DIR__) . '/bootstrap-staging.php';
}

class HVAC_Database_Test extends PHPUnit\Framework\TestCase {
    
    protected $test_post_id;
    
    protected function setUp(): void {
        parent::setUp();
        // Set staging environment if needed
        if (!getenv('HVAC_ENV')) {
            putenv('HVAC_ENV=staging');
        }
        
        // Create a test event post
        $this->test_post_id = create_test_event([
            'post_title' => 'Database Test Event',
            'post_content' => 'Test content for database operations'
        ]);
    }

    protected function tearDown(): void {
        // Clean up test post
        if ($this->test_post_id) {
            wp_delete_post($this->test_post_id, true);
        }
        parent::tearDown();
    }

    public function test_event_post_creation() {
        $post = get_post($this->test_post_id);
        
        $this->assertNotNull(
            $post,
            'Should be able to retrieve created event post'
        );
        
        $this->assertEquals(
            'Database Test Event',
            $post->post_title,
            'Event title should match created value'
        );
        
        $this->assertEquals(
            Tribe__Events__Main::POSTTYPE,
            $post->post_type,
            'Post type should be tribe_events'
        );
    }

    public function test_event_meta_operations() {
        // Test adding meta
        $result = add_post_meta(
            $this->test_post_id,
            '_EventStartDate',
            '2025-04-10 10:00:00'
        );
        
        $this->assertTrue(
            $result !== false,
            'Should successfully add event start date meta'
        );

        // Test getting meta
        $start_date = get_post_meta(
            $this->test_post_id,
            '_EventStartDate',
            true
        );
        
        $this->assertEquals(
            '2025-04-10 10:00:00',
            $start_date,
            'Should retrieve correct event start date'
        );

        // Test updating meta
        $result = update_post_meta(
            $this->test_post_id,
            '_EventStartDate',
            '2025-04-10 11:00:00'
        );
        
        $this->assertTrue(
            $result !== false,
            'Should successfully update event start date'
        );

        // Verify update
        $updated_date = get_post_meta(
            $this->test_post_id,
            '_EventStartDate',
            true
        );
        
        $this->assertEquals(
            '2025-04-10 11:00:00',
            $updated_date,
            'Should retrieve updated event start date'
        );
    }

    public function test_event_query() {
        // Create additional test events
        $event_ids = [];
        for ($i = 1; $i <= 3; $i++) {
            $event_ids[] = create_test_event([
                'post_title' => "Query Test Event $i"
            ]);
        }

        // Test WP_Query with event post type
        $query = new WP_Query([
            'post_type' => Tribe__Events__Main::POSTTYPE,
            'posts_per_page' => -1
        ]);

        $this->assertGreaterThanOrEqual(
            4, // Including the one from setUp
            $query->found_posts,
            'Should find all created test events'
        );

        // Clean up additional events
        foreach ($event_ids as $event_id) {
            wp_delete_post($event_id, true);
        }
    }

    public function test_venue_organizer_relationships() {
        // Create test venue
        $venue_id = wp_insert_post([
            'post_type' => Tribe__Events__Main::VENUE_POST_TYPE,
            'post_title' => 'Test Venue',
            'post_status' => 'publish'
        ]);

        // Create test organizer
        $organizer_id = wp_insert_post([
            'post_type' => Tribe__Events__Main::ORGANIZER_POST_TYPE,
            'post_title' => 'Test Organizer',
            'post_status' => 'publish'
        ]);

        // Link venue and organizer to event
        update_post_meta($this->test_post_id, '_EventVenueID', $venue_id);
        update_post_meta($this->test_post_id, '_EventOrganizerID', $organizer_id);

        // Test relationships
        $this->assertEquals(
            $venue_id,
            get_post_meta($this->test_post_id, '_EventVenueID', true),
            'Event should be linked to correct venue'
        );

        $this->assertEquals(
            $organizer_id,
            get_post_meta($this->test_post_id, '_EventOrganizerID', true),
            'Event should be linked to correct organizer'
        );

        // Clean up
        wp_delete_post($venue_id, true);
        wp_delete_post($organizer_id, true);
    }
}

// Add required function if not exists
if (!function_exists('wp_insert_post')) {
    function wp_insert_post($args, $wp_error = false) {
        static $post_id = 1000;
        error_log("[HVAC TEST] Mock wp_insert_post called with args: " . print_r($args, true));
        return $post_id++;
    }
}

if (!function_exists('wp_delete_post')) {
    function wp_delete_post($post_id, $force_delete = false) {
        error_log("[HVAC TEST] Mock wp_delete_post called for ID: $post_id");
        return true;
    }
}

if (!function_exists('get_post')) {
    function get_post($post_id) {
        error_log("[HVAC TEST] Mock get_post called for ID: $post_id");
        $post = new stdClass();
        $post->ID = $post_id;
        $post->post_type = Tribe__Events__Main::POSTTYPE;
        $post->post_title = 'Database Test Event';
        return $post;
    }
}

if (!class_exists('WP_Query')) {
    class WP_Query {
        public $found_posts = 4;
        public function __construct($args = []) {
            error_log("[HVAC TEST] Mock WP_Query constructed with args: " . print_r($args, true));
        }
    }
}