<?php

namespace App\Console\Commands;

use App\Domain\Subscription\ExpiryNotifier;
use App\Domain\Subscription\SubscriptionService;
use Illuminate\Console\Command;

class SubscriptionLifecycle extends Command
{
    protected $signature = 'medibook:subscriptions
        {--lifecycle : move expired subs to grace/suspended}
        {--notify : send multi-channel expiry notifications}';

    protected $description = 'Run subscription lifecycle transitions and expiry notifications.';

    public function handle(SubscriptionService $subs, ExpiryNotifier $notifier): int
    {
        if ($this->option('lifecycle') || ! $this->hasFlags()) {
            $r = $subs->runLifecycle();
            $this->line("→ grace: {$r['grace']}, suspended: {$r['suspended']}");
        }
        if ($this->option('notify') || ! $this->hasFlags()) {
            $this->line('expiry notifications: '.$notifier->run());
        }

        return self::SUCCESS;
    }

    protected function hasFlags(): bool
    {
        return $this->option('lifecycle') || $this->option('notify');
    }
}
