<?php
/**
 * Integration test for dashboard flow
 *
 * @package HVAC_Community_Events
 * @subpackage Tests
 */

/**
 * Class Test_Dashboard_Flow
 */
class Test_Dashboard_Flow extends WP_UnitTestCase {

	/**
	 * Test user
	 *
	 * @var WP_User
	 */
	private $trainer_user;

	/**
	 * Setup test
	 */
	public function setUp(): void {
		parent::setUp();
		
		// Create HVAC trainer role if it doesn't exist
		require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php';
		$roles = new HVAC_Roles();
		$roles->create_trainer_role();
		
		// Create test trainer user
		$this->trainer_user = new WP_User( $this->factory->user->create( array(
			'user_login' => 'test_trainer',
			'user_email' => 'trainer@example.com',
			'role' => 'hvac_trainer',
		) ) );
		
		// Set annual revenue target
		update_user_meta( $this->trainer_user->ID, 'annual_revenue_target', 50000 );
	}

	/**
	 * Teardown test
	 */
	public function tearDown(): void {
		// Clean up user
		wp_delete_user( $this->trainer_user->ID );
		
		parent::tearDown();
	}

	/**
	 * Test dashboard access control
	 */
	public function test_dashboard_access_control() {
		// Non-logged-in user should not have access
		$this->assertFalse( current_user_can( 'view_hvac_dashboard' ) );
		
		// Regular subscriber should not have access
		$subscriber = new WP_User( $this->factory->user->create( array(
			'role' => 'subscriber',
		) ) );
		wp_set_current_user( $subscriber->ID );
		$this->assertFalse( current_user_can( 'view_hvac_dashboard' ) );
		
		// Trainer should have access
		wp_set_current_user( $this->trainer_user->ID );
		$this->assertTrue( current_user_can( 'view_hvac_dashboard' ) );
		
		// Clean up
		wp_delete_user( $subscriber->ID );
		wp_set_current_user( 0 );
	}

	/**
	 * Test dashboard data loading
	 */
	public function test_dashboard_data_loading() {
		wp_set_current_user( $this->trainer_user->ID );
		
		// Load dashboard data
		require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-dashboard-data-refactored.php';
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->trainer_user->ID );
		
		// Get initial stats (should be empty)
		$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->assertEquals( 50000, $stats['revenue_target'] );
	}

	/**
	 * Test dashboard with events
	 */
	public function test_dashboard_with_events() {
		wp_set_current_user( $this->trainer_user->ID );
		
		// Create test events
		$events = array();
		
		// Past event
		$events[] = $this->factory->post->create( array(
			'post_type' => 'tribe_events',
			'post_status' => 'publish',
			'post_author' => $this->trainer_user->ID,
			'post_title' => 'Past Training Event',
		) );
		update_post_meta( $events[0], '_EventStartDate', date( 'Y-m-d H:i:s', strtotime( '-1 week' ) ) );
		update_post_meta( $events[0], '_EventEndDate', date( 'Y-m-d H:i:s', strtotime( '-1 week +3 hours' ) ) );
		update_post_meta( $events[0], '_tribe_tickets_sold', 15 );
		update_post_meta( $events[0], '_tribe_revenue_total', 1500 );
		
		// Future event
		$events[] = $this->factory->post->create( array(
			'post_type' => 'tribe_events',
			'post_status' => 'publish',
			'post_author' => $this->trainer_user->ID,
			'post_title' => 'Upcoming Training Event',
		) );
		update_post_meta( $events[1], '_EventStartDate', date( 'Y-m-d H:i:s', strtotime( '+1 week' ) ) );
		update_post_meta( $events[1], '_EventEndDate', date( 'Y-m-d H:i:s', strtotime( '+1 week +3 hours' ) ) );
		update_post_meta( $events[1], '_tribe_tickets_sold', 5 );
		update_post_meta( $events[1], '_tribe_revenue_total', 500 );
		
		// Draft event
		$events[] = $this->factory->post->create( array(
			'post_type' => 'tribe_events',
			'post_status' => 'draft',
			'post_author' => $this->trainer_user->ID,
			'post_title' => 'Draft Training Event',
		) );
		update_post_meta( $events[2], '_EventStartDate', date( 'Y-m-d H:i:s', strtotime( '+2 weeks' ) ) );
		update_post_meta( $events[2], '_tribe_tickets_sold', 0 );
		update_post_meta( $events[2], '_tribe_revenue_total', 0 );
		
		// Load dashboard data
		require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-dashboard-data-refactored.php';
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->trainer_user->ID );
		
		// Get stats
		$stats = $dashboard_data->get_all_stats();
		
		$this->assertEquals( 3, $stats['total_events'] );
		$this->assertEquals( 1, $stats['upcoming_events'] );
		$this->assertEquals( 1, $stats['past_events'] );
		$this->assertEquals( 20, $stats['total_tickets'] );
		$this->assertEquals( 2000, $stats['total_revenue'] );
		
		// Get events table data
		$table_data = $dashboard_data->get_events_table_data();
		$this->assertCount( 3, $table_data );
		
		// Clean up
		foreach ( $events as $event_id ) {
			wp_delete_post( $event_id, true );
		}
	}

	/**
	 * Test dashboard template rendering
	 */
	public function test_dashboard_template() {
		wp_set_current_user( $this->trainer_user->ID );
		
		// Capture output
		ob_start();
		include HVAC_CE_PLUGIN_DIR . 'templates/template-hvac-dashboard.php';
		$output = ob_get_clean();
		
		// Verify template elements
		$this->assertStringContainsString( 'Trainer Dashboard', $output );
		$this->assertStringContainsString( 'Your Stats', $output );
		$this->assertStringContainsString( 'Total Events', $output );
		$this->assertStringContainsString( 'Upcoming Events', $output );
		$this->assertStringContainsString( 'Total Revenue', $output );
		$this->assertStringContainsString( 'Your Events', $output );
	}

	/**
	 * Test admin area redirection
	 */
	public function test_admin_redirection() {
		// Trainers should be redirected from admin area
		wp_set_current_user( $this->trainer_user->ID );
		
		// The HVAC_Community_Events class should handle this
		$community_events = HVAC_Community_Events::instance();
		
		// Can't test actual redirect in unit tests, but can verify capability
		$this->assertTrue( current_user_can( 'view_hvac_dashboard' ) );
		$this->assertFalse( current_user_can( 'manage_options' ) );
	}

	/**
	 * Test revenue target display
	 */
	public function test_revenue_target_display() {
		wp_set_current_user( $this->trainer_user->ID );
		
		// Create an event with revenue
		$event_id = $this->factory->post->create( array(
			'post_type' => 'tribe_events',
			'post_status' => 'publish',
			'post_author' => $this->trainer_user->ID,
			'post_title' => 'Revenue Event',
		) );
		update_post_meta( $event_id, '_tribe_revenue_total', 5000 );
		
		// Load dashboard data
		require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-dashboard-data-refactored.php';
		$dashboard_data = new HVAC_Dashboard_Data_Refactored( $this->trainer_user->ID );
		$stats = $dashboard_data->get_all_stats();
		
		// Verify revenue and target
		$this->assertEquals( 5000, $stats['total_revenue'] );
		$this->assertEquals( 50000, $stats['revenue_target'] );
		
		// Revenue is 10% of target
		$percentage = ( $stats['total_revenue'] / $stats['revenue_target'] ) * 100;
		$this->assertEquals( 10, $percentage );
		
		// Clean up
		wp_delete_post( $event_id, true );
	}
}