<?php
/**
 * Unit Tests for HVAC Announcements Manager
 *
 * @package HVAC_Community_Events
 * @subpackage Tests
 */

class Test_HVAC_Announcements_Manager extends WP_UnitTestCase {
    
    /**
     * Manager instance
     */
    private $manager;
    
    /**
     * Test users
     */
    private $master_trainer;
    private $regular_trainer;
    
    /**
     * Original page ID backup
     */
    private $original_page_id;
    
    /**
     * Setup before each test
     */
    public function setUp() {
        parent::setUp();
        
        // Create test users
        $this->master_trainer = $this->factory->user->create( array(
            'role' => 'hvac_master_trainer',
            'user_login' => 'master_trainer',
            'user_email' => 'master@example.com',
        ) );
        
        $this->regular_trainer = $this->factory->user->create( array(
            'role' => 'hvac_trainer',
            'user_login' => 'regular_trainer',
            'user_email' => 'trainer@example.com',
        ) );
        
        // Add capabilities
        HVAC_Announcements_Permissions::add_capabilities();
        
        // Get manager instance
        $this->manager = HVAC_Announcements_Manager::get_instance();
        
        // Backup original page ID
        $this->original_page_id = get_option( 'hvac_trainer_resources_page_id' );
    }
    
    /**
     * Teardown after each test
     */
    public function tearDown() {
        parent::tearDown();
        
        // Clean up test users
        wp_delete_user( $this->master_trainer );
        wp_delete_user( $this->regular_trainer );
        
        // Remove capabilities
        HVAC_Announcements_Permissions::remove_capabilities();
        
        // Restore original page ID
        if ( $this->original_page_id ) {
            update_option( 'hvac_trainer_resources_page_id', $this->original_page_id );
        } else {
            delete_option( 'hvac_trainer_resources_page_id' );
        }
    }
    
    /**
     * Test singleton pattern
     */
    public function test_singleton_pattern() {
        $instance1 = HVAC_Announcements_Manager::get_instance();
        $instance2 = HVAC_Announcements_Manager::get_instance();
        
        $this->assertSame( $instance1, $instance2 );
    }
    
    /**
     * Test component initialization
     */
    public function test_component_initialization() {
        // Test that all components are instantiated
        $this->assertInstanceOf( 'HVAC_Announcements_CPT', HVAC_Announcements_CPT::get_instance() );
        $this->assertInstanceOf( 'HVAC_Announcements_Permissions', HVAC_Announcements_Permissions::get_instance() );
        $this->assertInstanceOf( 'HVAC_Announcements_Display', HVAC_Announcements_Display::get_instance() );
        $this->assertInstanceOf( 'HVAC_Announcements_Ajax', HVAC_Announcements_Ajax::get_instance() );
        $this->assertInstanceOf( 'HVAC_Announcements_Email', HVAC_Announcements_Email::get_instance() );
    }
    
    /**
     * Test create resources page
     */
    public function test_create_resources_page() {
        // Delete existing page if any
        $existing_page_id = get_option( 'hvac_trainer_resources_page_id' );
        if ( $existing_page_id ) {
            wp_delete_post( $existing_page_id, true );
            delete_option( 'hvac_trainer_resources_page_id' );
        }
        
        // Create resources page
        $reflection = new ReflectionClass( $this->manager );
        $method = $reflection->getMethod( 'create_resources_page' );
        $method->setAccessible( true );
        $method->invoke( $this->manager );
        
        // Check page was created
        $page_id = get_option( 'hvac_trainer_resources_page_id' );
        $this->assertNotEmpty( $page_id );
        
        $page = get_post( $page_id );
        $this->assertNotNull( $page );
        $this->assertEquals( 'page', $page->post_type );
        $this->assertEquals( 'Training Resources', $page->post_title );
        $this->assertStringContainsString( '[hvac_announcements_timeline]', $page->post_content );
        
        // Clean up
        wp_delete_post( $page_id, true );
        delete_option( 'hvac_trainer_resources_page_id' );
    }
    
    /**
     * Test create master announcements page
     */
    public function test_create_master_announcements_page() {
        // Delete existing page if any
        $existing_page_id = get_option( 'hvac_master_announcements_page_id' );
        if ( $existing_page_id ) {
            wp_delete_post( $existing_page_id, true );
            delete_option( 'hvac_master_announcements_page_id' );
        }
        
        // Create master announcements page
        $reflection = new ReflectionClass( $this->manager );
        $method = $reflection->getMethod( 'create_master_announcements_page' );
        $method->setAccessible( true );
        $method->invoke( $this->manager );
        
        // Check page was created
        $page_id = get_option( 'hvac_master_announcements_page_id' );
        $this->assertNotEmpty( $page_id );
        
        $page = get_post( $page_id );
        $this->assertNotNull( $page );
        $this->assertEquals( 'page', $page->post_type );
        $this->assertEquals( 'Manage Announcements', $page->post_title );
        
        // Clean up
        wp_delete_post( $page_id, true );
        delete_option( 'hvac_master_announcements_page_id' );
    }
    
    /**
     * Test should enqueue admin scripts
     */
    public function test_should_enqueue_admin_scripts() {
        $reflection = new ReflectionClass( $this->manager );
        $method = $reflection->getMethod( 'should_enqueue_admin_scripts' );
        $method->setAccessible( true );
        
        // Test on non-announcement page
        set_current_screen( 'dashboard' );
        $this->assertFalse( $method->invoke( $this->manager ) );
        
        // Test on master announcements page
        $page_id = $this->factory->post->create( array(
            'post_type' => 'page',
            'post_title' => 'Test Page',
        ) );
        update_option( 'hvac_master_announcements_page_id', $page_id );
        
        $_GET['page_id'] = $page_id;
        // Note: In actual WordPress environment, this would return true
        // but in test environment, current_screen might not be set properly
        
        // Clean up
        unset( $_GET['page_id'] );
        wp_delete_post( $page_id, true );
        delete_option( 'hvac_master_announcements_page_id' );
    }
    
    /**
     * Test activation hook
     */
    public function test_activation() {
        // Run activation
        HVAC_Announcements_Manager::activate();
        
        // Check that capabilities were added
        $master_role = get_role( 'hvac_master_trainer' );
        if ( $master_role ) {
            $this->assertTrue( $master_role->has_cap( 'read_hvac_announcements' ) );
            $this->assertTrue( $master_role->has_cap( 'publish_hvac_announcements' ) );
        }
        
        // Check that pages were created
        $resources_page_id = get_option( 'hvac_trainer_resources_page_id' );
        $master_page_id = get_option( 'hvac_master_announcements_page_id' );
        
        if ( $resources_page_id ) {
            $page = get_post( $resources_page_id );
            $this->assertNotNull( $page );
        }
        
        if ( $master_page_id ) {
            $page = get_post( $master_page_id );
            $this->assertNotNull( $page );
        }
    }
    
    /**
     * Test deactivation hook
     */
    public function test_deactivation() {
        // Add capabilities first
        HVAC_Announcements_Permissions::add_capabilities();
        
        // Run deactivation
        HVAC_Announcements_Manager::deactivate();
        
        // Check that capabilities were removed
        $master_role = get_role( 'hvac_master_trainer' );
        if ( $master_role ) {
            $this->assertFalse( $master_role->has_cap( 'read_hvac_announcements' ) );
            $this->assertFalse( $master_role->has_cap( 'publish_hvac_announcements' ) );
        }
    }
    
    /**
     * Test enqueue admin scripts
     */
    public function test_enqueue_admin_scripts() {
        // Set current user as master trainer
        wp_set_current_user( $this->master_trainer );
        
        // Enqueue scripts
        $this->manager->enqueue_admin_scripts();
        
        // Check if scripts are enqueued
        $this->assertTrue( wp_script_is( 'hvac-announcements-admin', 'enqueued' ) );
        $this->assertTrue( wp_style_is( 'hvac-announcements-admin', 'enqueued' ) );
        
        // Check localization
        $localized = wp_scripts()->get_data( 'hvac-announcements-admin', 'data' );
        $this->assertNotEmpty( $localized );
        $this->assertStringContainsString( 'hvac_announcements_admin', $localized );
    }
    
    /**
     * Test check permissions for menu
     */
    public function test_check_permissions_for_menu() {
        // Test as regular trainer
        wp_set_current_user( $this->regular_trainer );
        $reflection = new ReflectionClass( $this->manager );
        $method = $reflection->getMethod( 'check_permissions' );
        $method->setAccessible( true );
        
        ob_start();
        $method->invoke( $this->manager );
        $output = ob_get_clean();
        
        $this->assertStringContainsString( 'You do not have permission', $output );
        
        // Test as master trainer
        wp_set_current_user( $this->master_trainer );
        
        ob_start();
        $method->invoke( $this->manager );
        $output = ob_get_clean();
        
        // Should not show error for master trainer
        $this->assertStringNotContainsString( 'You do not have permission', $output );
    }
    
    /**
     * Test page template assignment
     */
    public function test_page_template_assignment() {
        // Create a test page
        $page_id = $this->factory->post->create( array(
            'post_type' => 'page',
            'post_title' => 'Test Resources Page',
        ) );
        
        // Set as resources page
        update_option( 'hvac_trainer_resources_page_id', $page_id );
        
        // Run the template assignment
        $reflection = new ReflectionClass( $this->manager );
        $method = $reflection->getMethod( 'create_resources_page' );
        $method->setAccessible( true );
        $method->invoke( $this->manager );
        
        // Check template was assigned
        $template = get_post_meta( $page_id, '_wp_page_template', true );
        $this->assertEquals( 'templates/page-trainer-resources.php', $template );
        
        // Clean up
        wp_delete_post( $page_id, true );
    }
}