<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ListActiveUsers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'users:list-active';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'List all active users from the database for testing search functionality';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('========================================');
        $this->info('  REAL USERS IN YOUR DATABASE');
        $this->info('========================================');
        $this->info('');
        
        try {
            // Count active users
            $totalActive = \App\User::where('status', 'active')
                ->whereNull('deleted_at')
                ->count();
            
            $this->info("Total Active Users: {$totalActive}");
            $this->info('');
            
            if ($totalActive == 0) {
                $this->warn('⚠️  No active users found!');
                $this->line('   Users need to have:');
                $this->line('   - status = "active"');
                $this->line('   - deleted_at = NULL');
                $this->info('');
            } else {
                // Get first 20 users
                $this->info('First 20 Active Users:');
                $this->line(str_repeat('-', 70));
                
                $users = \App\User::where('status', 'active')
                    ->whereNull('deleted_at')
                    ->orderBy('full_name', 'asc')
                    ->take(20)
                    ->get(['id', 'full_name', 'email', 'role_name']);
                
                foreach ($users as $user) {
                    $name = $user->full_name ?: '(No Name)';
                    $email = $user->email ?: '(No Email)';
                    $role = $user->role_name ?: 'N/A';
                    $this->line(sprintf("%-5s | %-25s | %-30s | %-15s", 
                        $user->id, 
                        substr($name, 0, 25), 
                        substr($email, 0, 30),
                        $role
                    ));
                }
                
                $this->line(str_repeat('-', 70));
                $this->info('');
                
                // Show users grouped by role
                $this->info('Users by Role:');
                $usersByRole = \App\User::where('status', 'active')
                    ->whereNull('deleted_at')
                    ->select('role_name', \DB::raw('count(*) as count'))
                    ->groupBy('role_name')
                    ->get();
                
                foreach ($usersByRole as $role) {
                    $this->line("  - {$role->role_name}: {$role->count} users");
                }
                
                $this->info('');
                $this->info('✅ These are the users that will appear in the search!');
                $this->info('');
                $this->line('To test the search:');
                $this->line('1. Go to webinar create/edit page');
                $this->line('2. Open any accordion with "Allowed Users"');
                $this->line('3. Click dropdown or start typing a name/email');
                $this->line('4. You should see these users appear!');
            }
            
            return 0;
        } catch (\Exception $e) {
            $this->error('Error: ' . $e->getMessage());
            return 1;
        }
    }
}
