<?php
/**
 * Tests for HVAC_Dashboard_Data_Refactored class
 *
 * @package HVAC_Community_Events
 * @subpackage Tests
 */

/**
 * Class HVAC_Dashboard_Data_Test
 */
class HVAC_Dashboard_Data_Test extends WP_UnitTestCase {

	/**
	 * Test user ID
	 *
	 * @var int
	 */
	private $user_id;

	/**
	 * Test events
	 *
	 * @var array
	 */
	private $test_events = array();

	/**
	 * Setup test
	 */
	public function setUp(): void {
		parent::setUp();
		
		// Create test user
		$this->user_id = $this->factory->user->create( array(
			'role' => 'hvac_trainer',
		) );
		
		// Mock The Events Calendar constants
		if ( ! defined( 'Tribe__Events__Main' ) ) {
			define( 'Tribe__Events__Main', 'MockTribeEvents' );
		}
		if ( ! defined( 'MockTribeEvents::POSTTYPE' ) ) {
			define( 'MockTribeEvents::POSTTYPE', 'tribe_events' );
		}
	}

	/**
	 * Teardown test
	 */
	public function tearDown(): void {
		// Clean up test events
		foreach ( $this->test_events as $event_id ) {
			wp_delete_post( $event_id, true );
		}
		
		// Clean up test user
		wp_delete_user( $this->user_id );
		
		parent::tearDown();
	}

	/**
	 * Create test event
	 *
	 * @param array $args Event arguments
	 * @return int Event ID
	 */
	private function create_test_event( $args = array() ) {
		$defaults = array(
			'post_type' => MockTribeEvents::POSTTYPE,
			'post_status' => 'publish',
			'post_author' => $this->user_id,
			'post_title' => 'Test Event',
		);
		
		$args = wp_parse_args( $args, $defaults );
		$event_id = $this->factory->post->create( $args );
		
		// Add default meta data
		$meta_defaults = array(
			'_EventStartDate' => current_time( 'mysql' ),
			'_EventEndDate' => current_time( 'mysql' ),
			'_EventOrganizerID' => $this->user_id,
			'_tribe_tickets_sold' => 0,
			'_tribe_revenue_total' => 0,
		);
		
		foreach ( $meta_defaults as $key => $value ) {
			if ( ! isset( $args['meta'][ $key ] ) ) {
				update_post_meta( $event_id, $key, $value );
			}
		}
		
		// Apply custom meta
		if ( isset( $args['meta'] ) ) {
			foreach ( $args['meta'] as $key => $value ) {
				update_post_meta( $event_id, $key, $value );
			}
		}
		
		$this->test_events[] = $event_id;
		return $event_id;
	}

	/**
	 * Test dashboard data initialization
	 */
	public function test_initialization() {
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$this->assertInstanceOf( 'HVAC_Dashboard_Data_Refactored', $dashboard_data );
	}

	/**
	 * Test get all stats with caching
	 */
	public function test_get_all_stats() {
		// Create test events
		$this->create_test_event( array(
			'meta' => array(
				'_EventStartDate' => date( 'Y-m-d H:i:s', strtotime( '+1 week' ) ),
				'_tribe_tickets_sold' => 10,
				'_tribe_revenue_total' => 500,
			),
		) );
		
		$this->create_test_event( array(
			'meta' => array(
				'_EventStartDate' => date( 'Y-m-d H:i:s', strtotime( '-1 week' ) ),
				'_tribe_tickets_sold' => 5,
				'_tribe_revenue_total' => 250,
			),
		) );
		
		// Set revenue target
		update_user_meta( $this->user_id, 'annual_revenue_target', 10000 );
		
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$stats = $dashboard_data->get_all_stats();
		
		$this->assertIsArray( $stats );
		$this->assertArrayHasKey( 'total_events', $stats );
		$this->assertArrayHasKey( 'upcoming_events', $stats );
		$this->assertArrayHasKey( 'past_events', $stats );
		$this->assertArrayHasKey( 'total_tickets', $stats );
		$this->assertArrayHasKey( 'total_revenue', $stats );
		$this->assertArrayHasKey( 'revenue_target', $stats );
		
		$this->assertEquals( 2, $stats['total_events'] );
		$this->assertEquals( 15, $stats['total_tickets'] );
		$this->assertEquals( 750, $stats['total_revenue'] );
		$this->assertEquals( 10000, $stats['revenue_target'] );
		
		// Test caching - should return same result without recalculating
		$stats2 = $dashboard_data->get_all_stats();
		$this->assertEquals( $stats, $stats2 );
	}

	/**
	 * Test cache clearing
	 */
	public function test_cache_clearing() {
		$this->create_test_event();
		
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$stats1 = $dashboard_data->get_all_stats();
		
		// Clear cache
		$dashboard_data->clear_cache();
		
		// Create another event
		$this->create_test_event();
		
		// Should recalculate and get different results
		$stats2 = $dashboard_data->get_all_stats();
		$this->assertNotEquals( $stats1['total_events'], $stats2['total_events'] );
	}

	/**
	 * Test upcoming events count
	 */
	public function test_upcoming_events_count() {
		// Create past event
		$this->create_test_event( array(
			'meta' => array(
				'_EventStartDate' => date( 'Y-m-d H:i:s', strtotime( '-1 week' ) ),
				'_EventEndDate' => date( 'Y-m-d H:i:s', strtotime( '-1 week +2 hours' ) ),
			),
		) );
		
		// Create future event
		$this->create_test_event( array(
			'meta' => array(
				'_EventStartDate' => date( 'Y-m-d H:i:s', strtotime( '+1 week' ) ),
				'_EventEndDate' => date( 'Y-m-d H:i:s', strtotime( '+1 week +2 hours' ) ),
			),
		) );
		
		// Create current event
		$this->create_test_event( array(
			'meta' => array(
				'_EventStartDate' => date( 'Y-m-d H:i:s', strtotime( '-1 hour' ) ),
				'_EventEndDate' => date( 'Y-m-d H:i:s', strtotime( '+1 hour' ) ),
			),
		) );
		
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$stats = $dashboard_data->get_all_stats();
		
		// Only the future event should count as upcoming
		$this->assertEquals( 1, $stats['upcoming_events'] );
		$this->assertEquals( 1, $stats['past_events'] );
	}

	/**
	 * Test events table data
	 */
	public function test_get_events_table_data() {
		// Create various status events
		$this->create_test_event( array(
			'post_status' => 'publish',
			'post_title' => 'Published Event',
		) );
		
		$this->create_test_event( array(
			'post_status' => 'draft',
			'post_title' => 'Draft Event',
		) );
		
		$this->create_test_event( array(
			'post_status' => 'future',
			'post_title' => 'Scheduled Event',
		) );
		
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		
		// Get all events
		$all_events = $dashboard_data->get_events_table_data( 'all' );
		$this->assertCount( 3, $all_events );
		
		// Get only published events
		$published_events = $dashboard_data->get_events_table_data( 'publish' );
		$this->assertCount( 1, $published_events );
		$this->assertEquals( 'publish', $published_events[0]['status'] );
		
		// Get only draft events
		$draft_events = $dashboard_data->get_events_table_data( 'draft' );
		$this->assertCount( 1, $draft_events );
		$this->assertEquals( 'draft', $draft_events[0]['status'] );
		
		// Verify event data structure
		$event = $all_events[0];
		$this->assertArrayHasKey( 'id', $event );
		$this->assertArrayHasKey( 'status', $event );
		$this->assertArrayHasKey( 'name', $event );
		$this->assertArrayHasKey( 'link', $event );
		$this->assertArrayHasKey( 'start_date_ts', $event );
		$this->assertArrayHasKey( 'organizer_id', $event );
		$this->assertArrayHasKey( 'capacity', $event );
		$this->assertArrayHasKey( 'sold', $event );
		$this->assertArrayHasKey( 'revenue', $event );
	}

	/**
	 * Test revenue calculation
	 */
	public function test_revenue_calculation() {
		// Create events with different revenue
		$this->create_test_event( array(
			'meta' => array(
				'_tribe_revenue_total' => 1000.50,
			),
		) );
		
		$this->create_test_event( array(
			'meta' => array(
				'_tribe_revenue_total' => 2500.75,
			),
		) );
		
		$this->create_test_event( array(
			'meta' => array(
				'_tribe_revenue_total' => 0,
			),
		) );
		
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$stats = $dashboard_data->get_all_stats();
		
		$this->assertEquals( 3501.25, $stats['total_revenue'] );
	}

	/**
	 * Test tickets sold calculation
	 */
	public function test_tickets_sold_calculation() {
		// Create events with different ticket counts
		$this->create_test_event( array(
			'meta' => array(
				'_tribe_tickets_sold' => 10,
			),
		) );
		
		$this->create_test_event( array(
			'meta' => array(
				'_tribe_tickets_sold' => 25,
			),
		) );
		
		$this->create_test_event( array(
			'meta' => array(
				'_tribe_tickets_sold' => 0,
			),
		) );
		
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$stats = $dashboard_data->get_all_stats();
		
		$this->assertEquals( 35, $stats['total_tickets'] );
	}

	/**
	 * Test with no events
	 */
	public function test_with_no_events() {
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->user_id );
		$stats = $dashboard_data->get_all_stats();
		
		$this->assertEquals( 0, $stats['total_events'] );
		$this->assertEquals( 0, $stats['upcoming_events'] );
		$this->assertEquals( 0, $stats['past_events'] );
		$this->assertEquals( 0, $stats['total_tickets'] );
		$this->assertEquals( 0, $stats['total_revenue'] );
		$this->assertNull( $stats['revenue_target'] );
		
		$events = $dashboard_data->get_events_table_data();
		$this->assertIsArray( $events );
		$this->assertEmpty( $events );
	}
}