<?php

namespace App\Console\Commands;

use App\Domains\Billing\Actions\SyncTenantLifecycleAction;
use App\Domains\Tenancy\Models\Tenant;
use Illuminate\Console\Command;

class SyncTenantLifecycleCommand extends Command
{
    protected $signature = 'tenants:sync-lifecycle';

    protected $description = 'Sync tenant statuses from subscription and trial state';

    public function handle(SyncTenantLifecycleAction $syncTenantLifecycle): int
    {
        $count = 0;

        Tenant::query()->chunk(100, function ($tenants) use ($syncTenantLifecycle, &$count) {
            foreach ($tenants as $tenant) {
                $syncTenantLifecycle->execute($tenant);
                $count++;
            }
        });

        $this->info("Synced lifecycle for {$count} tenants.");

        return self::SUCCESS;
    }
}
