<?php

declare(strict_types=1);

namespace App\Console\Commands;

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

final class RollbackSitemapReleaseCommand extends Command
{
    protected $signature = 'skinbase:sitemaps:rollback {release? : Release id to activate instead of the previous published release}';

    protected $description = 'Rollback sitemap delivery to a previous published release.';

    public function handle(SitemapPublishService $publish): int
    {
        try {
            $manifest = $publish->rollback(($release = $this->argument('release')) !== null ? (string) $release : null);
        } catch (\Throwable $exception) {
            $this->error($exception->getMessage());

            return self::FAILURE;
        }

        $this->info('Rolled back to sitemap release [' . (string) $manifest['release_id'] . '].');

        return self::SUCCESS;
    }
}