<?php

namespace Modules\PortalApi\Console;

use Illuminate\Console\Command;
use Modules\PortalApi\Entities\PortalOwner;
use Modules\PortalApi\Services\Push\PortalPushSender;

/**
 *   php artisan portal:push:test 1
 *
 * Sends a hello-world push to every subscription of portal_owners.id = 1.
 */
class TestPushCommand extends Command
{
    protected $signature   = 'portal:push:test {ownerId}';
    protected $description = 'Send a test push notification to every subscription of the given portal owner.';

    public function handle(PortalPushSender $sender): int
    {
        $owner = PortalOwner::find($this->argument('ownerId'));
        if (! $owner) {
            $this->error('Owner not found.');
            return self::FAILURE;
        }

        $count = $sender->sendToOwner($owner->id, 'vaccinations', [
            'title' => "Hi {$owner->name}!",
            'body'  => 'This is a test push from your pet portal.',
            'url'   => '/app/home',
            'icon'  => '/app/icon-192.png',
        ]);

        $this->info($count
            ? "Sent to {$count} subscription(s)."
            : 'No subscriptions matched (owner hasn\'t opted in or library not installed yet).');

        if (! $sender->isReady()) {
            $this->newLine();
            $this->comment('Push library not yet active — payload logged but not actually sent.');
            $this->comment('Run:  composer require minishlink/web-push  to enable real delivery.');
        }
        return self::SUCCESS;
    }
}
