<?php
/**
 * Creates test users with specific roles and personas
 */
class HVAC_Test_User_Factory {
    private static $instance;
    private $created_users = [];
    private $role_manager;

    private function __construct() {
        $this->role_manager = HVAC_Role_Manager::get_instance();
    }

    public static function get_instance() {
        if (!isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Create a test user with specified role
     */
    public function create_user($username, $role = 'subscriber', $persona = 'basic') {
        $user_id = wp_create_user($username, 'password', $username . '@example.com');
        
        if (is_wp_error($user_id)) {
            return false;
        }

        $user = new WP_User($user_id);
        $user->set_role($role);
        
        $this->created_users[] = [
            'id' => $user_id,
            'username' => $username,
            'role' => $role,
            'persona' => $persona
        ];

        return $user_id;
    }

    /**
     * Create a user with a custom role and capabilities
     */
    public function create_user_with_custom_role($username, $role, $display_name, $capabilities = []) {
        $this->role_manager->create_role($role, $display_name, $capabilities);
        return $this->create_user($username, $role);
    }

    /**
     * Create a user with a predefined persona
     */
    public function create_user_with_persona($username, $persona) {
        $personas = $this->get_personas();
        
        if (!isset($personas[$persona])) {
            return false;
        }

        $persona_data = $personas[$persona];
        return $this->create_user_with_custom_role(
            $username,
            $persona_data['role'],
            $persona_data['display_name'],
            $persona_data['capabilities']
        );
    }

    /**
     * Get predefined user personas
     */
    public function get_personas() {
        return [
            'basic_trainer' => [
                'role' => 'hvac_trainer_basic',
                'display_name' => 'Basic HVAC Trainer',
                'capabilities' => [
                    'edit_events' => true,
                    'publish_events' => false
                ]
            ],
            'advanced_trainer' => [
                'role' => 'hvac_trainer_advanced',
                'display_name' => 'Advanced HVAC Trainer',
                'capabilities' => [
                    'edit_events' => true,
                    'publish_events' => true,
                    'manage_attendees' => true
                ]
            ],
            'admin' => [
                'role' => 'hvac_admin',
                'display_name' => 'HVAC Administrator',
                'capabilities' => [
                    'manage_options' => true,
                    'edit_users' => true
                ]
            ]
        ];
    }

    /**
     * Clean up all created users and roles
     */
    public function cleanup() {
        foreach ($this->created_users as $user) {
            wp_delete_user($user['id']);
        }
        $this->created_users = [];
        $this->role_manager->cleanup();
    }
}