<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Models\User;
use Hypervel\Console\Command;

class ResetAllUsersCommand extends Command
{
    protected ?string $signature = 'app:reset-users {--message= : Optional toast message shown before reload}';

    protected string $description = 'Queue a browser reload for all users on their next SSE tick (next login or active session).';

    public function handle()
    {
        $message = $this->option('message') ?: 'App update applied. Reloading...';

        $js = "sessionStorage.clear(); alert('" . addslashes($message) . "'); window.location.reload();";

        $affected = User::whereNotNull('id')->update(['exec_command' => $js]);

        $this->info("Reset command queued for {$affected} users.");
        $this->line("Command: <comment>{$js}</comment>");
    }
}
