<?php

namespace Modules\ClinicStaffPortal\Console;

use App\User;
use Illuminate\Console\Command;
use Modules\ClinicStaffPortal\Services\StaffPushSender;

/**
 * Send a test push to a staff user's devices.
 *   php artisan staff-portal:push-test {user_id}
 */
class PushStaffTestCommand extends Command
{
    protected $signature   = 'staff-portal:push-test {user : ERP user id}';
    protected $description = 'Send a test push notification to a staff user';

    public function handle(StaffPushSender $sender): int
    {
        $user = User::find($this->argument('user'));
        if (! $user) { $this->error('User not found.'); return self::FAILURE; }

        $base = rtrim(env('STAFF_PWA_URL', url('/staff-app/')), '/');
        $sent = $sender->sendToUser((int) $user->id, 'alerts', [
            'title' => '🩺 Test notification',
            'body'  => 'If you can see this, staff push is working.',
            'url'   => $base . '/schedule',
            'icon'  => $base . '/icon-192.png',
            'tag'   => 'staff-test',
        ]);

        $this->info("Dispatched to {$sent} device(s) for {$user->username}.");
        if (! $sender->isReady()) {
            $this->warn('Note: minishlink/web-push not installed or VAPID keys unset — delivery was a logged no-op.');
        }
        return self::SUCCESS;
    }
}
