<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Services\Sitemaps\SitemapReleaseManager;
use Illuminate\Console\Command;

final class ListSitemapReleasesCommand extends Command
{
    protected $signature = 'skinbase:sitemaps:releases';

    protected $description = 'List recent sitemap releases and the active release.';

    public function handle(SitemapReleaseManager $releases): int
    {
        $active = $releases->activeReleaseId();

        foreach ($releases->listReleases() as $release) {
            $this->line(sprintf(
                '%s status=%s families=%d published_at=%s%s',
                (string) ($release['release_id'] ?? 'unknown'),
                (string) ($release['status'] ?? 'unknown'),
                (int) data_get($release, 'totals.families', 0),
                (string) ($release['published_at'] ?? 'n/a'),
                (string) ($release['release_id'] ?? '') === $active ? ' [active]' : '',
            ));
        }

        return self::SUCCESS;
    }
}