<?php
defined('_ISVALID') or exit;

class VUserManager {
    public static function createTestUsers() {
        global $class_database;
        
        // Check if test users exist
        $existing = $class_database->execute("SELECT COUNT(*) as count FROM db_accountuser");
        $user_count = $existing[0]['count'] ?? 0;
        
        if ($user_count < 3) {
            // Create test users
            $test_users = [
                [
                    'username' => 'admin',
                    'email' => 'admin@easystream.local',
                    'password' => md5('admin123'),
                    'user_type' => 'admin',
                    'verified' => 1
                ],
                [
                    'username' => 'creator1',
                    'email' => 'creator@easystream.local',
                    'password' => md5('creator123'),
                    'user_type' => 'member',
                    'verified' => 1
                ],
                [
                    'username' => 'viewer1',
                    'email' => 'viewer@easystream.local',
                    'password' => md5('viewer123'),
                    'user_type' => 'member',
                    'verified' => 1
                ]
            ];
            
            foreach ($test_users as $user) {
                $sql = "INSERT INTO db_accountuser (
                    usr_user, usr_email, usr_password, usr_type, usr_verified, 
                    signup_date, usr_active, usr_key
                ) VALUES (?, ?, ?, ?, ?, NOW(), 1, ?)";
                
                $user_key = uniqid() . time();
                $class_database->execute($sql, [
                    $user['username'], $user['email'], $user['password'],
                    $user['user_type'], $user['verified'], $user_key
                ]);
            }
            
            return "Created test users: admin/admin123, creator1/creator123, viewer1/viewer123";
        }
        
        return "Test users already exist";
    }
    
    public static function getUserStats() {
        global $class_database;
        
        $stats = [];
        
        // Total users
        $result = $class_database->execute("SELECT COUNT(*) as count FROM db_accountuser WHERE usr_active = 1");
        $stats['total_users'] = $result[0]['count'] ?? 0;
        
        // New users today
        $result = $class_database->execute("SELECT COUNT(*) as count FROM db_accountuser WHERE DATE(signup_date) = CURDATE()");
        $stats['new_today'] = $result[0]['count'] ?? 0;
        
        // Online users (active in last 30 minutes)
        $result = $class_database->execute("SELECT COUNT(*) as count FROM db_accountuser WHERE usr_lastlogin > DATE_SUB(NOW(), INTERVAL 30 MINUTE)");
        $stats['online_now'] = $result[0]['count'] ?? 0;
        
        return $stats;
    }
    
    public static function getRecentUsers($limit = 10) {
        global $class_database;
        
        $sql = "SELECT usr_user, usr_email, signup_date, usr_lastlogin, usr_type 
                FROM db_accountuser 
                WHERE usr_active = 1 
                ORDER BY signup_date DESC 
                LIMIT ?";
                
        return $class_database->execute($sql, [$limit]);
    }
}
?>