<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Models\User;

class CheckSeededData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check:seeded-data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check the seeded roles, permissions, and users';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('=== ROLES ===');
        $roles = Role::all(['name']);
        foreach ($roles as $role) {
            $this->line("- {$role->name}");
        }

        $this->info("\n=== PERMISSIONS ===");
        $permissions = Permission::all(['name']);
        foreach ($permissions as $permission) {
            $this->line("- {$permission->name}");
        }

        $this->info("\n=== USERS WITH ROLES ===");
        $users = User::with('roles')->get(['name', 'email', 'role_type']);
        foreach ($users as $user) {
            $roleType = match($user->role_type) {
                1 => 'landlord',
                2 => 'tenant', 
                3 => 'admin',
                default => 'unknown'
            };
            $roles = $user->roles->pluck('name')->join(', ');
            $this->line("- {$user->name} ({$user->email}) - Type: {$roleType} - Roles: {$roles}");
        }

        $this->info("\n=== ROLE PERMISSIONS ===");
        $allRoles = Role::all();
        foreach ($allRoles as $role) {
            $permissions = $role->permissions->pluck('name');
            $this->line("\n{$role->name}:");
            foreach ($permissions as $permission) {
                $this->line("  - {$permission}");
            }
        }

        return 0;
    }
}