<?php

class HVAC_Test_User_Factory_Test extends WP_UnitTestCase {

    public function test_user_creation_with_single_role() {
        $role = 'test_role_' . uniqid();
        HVAC_Role_Manager::create_role($role);
        
        $user = HVAC_Test_User_Factory::create_user([
            'username' => 'testuser_' . uniqid(),
            'roles' => [$role]
        ]);
        
        $this->assertInstanceOf('WP_User', $user);
        $this->assertTrue(in_array($role, $user->roles));
    }

    public function test_user_creation_with_multiple_roles() {
        $roles = [
            'test_role1_' . uniqid(),
            'test_role2_' . uniqid()
        ];
        
        foreach ($roles as $role) {
            HVAC_Role_Manager::create_role($role);
        }
        
        $user = HVAC_Test_User_Factory::create_user([
            'username' => 'testuser_' . uniqid(),
            'roles' => $roles
        ]);
        
        $this->assertInstanceOf('WP_User', $user);
        foreach ($roles as $role) {
            $this->assertTrue(in_array($role, $user->roles));
        }
    }

    public function test_persona_management() {
        $persona = 'test_persona_' . uniqid();
        $roles = ['test_role1_' . uniqid(), 'test_role2_' . uniqid()];
        
        HVAC_Test_User_Factory::define_persona($persona, [
            'roles' => $roles,
            'meta' => [
                'test_meta' => 'value'
            ]
        ]);
        
        $user = HVAC_Test_User_Factory::create_user([
            'username' => 'testuser_' . uniqid(),
            'persona' => $persona
        ]);
        
        $this->assertInstanceOf('WP_User', $user);
        foreach ($roles as $role) {
            $this->assertTrue(in_array($role, $user->roles));
        }
        $this->assertEquals('value', get_user_meta($user->ID, 'test_meta', true));
    }

    public function test_account_cleanup() {
        $role = 'test_cleanup_role_' . uniqid();
        HVAC_Role_Manager::create_role($role);
        
        $user = HVAC_Test_User_Factory::create_user([
            'username' => 'testuser_' . uniqid(),
            'roles' => [$role]
        ]);
        
        HVAC_Test_User_Factory::cleanup_user($user->ID);
        
        $this->assertFalse(HVAC_Role_Manager::role_exists($role));
        $this->assertFalse(get_user_by('id', $user->ID));
    }
}