<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Business\Product\ProductSkuLineBackfillService;
use Illuminate\Console\Command;

final class DberpProductSkuLineBackfillCommand extends Command
{
    protected $signature = 'dberp:product-sku-line-backfill
                            {--dry-run : 仅统计，不写库}
                            {--report : 生成 JSON 治理报告文件}';

    protected $description = '按「仅一条启用规格组合」规则安全回填明细行 product_sku_id 与规格快照，并输出需人工确认的例外清单';

    public function handle(ProductSkuLineBackfillService $service): int
    {
        $dryRun = (bool) $this->option('dry-run');
        $writeReport = (bool) $this->option('report');

        $result = $service->run($dryRun, $writeReport);

        $this->info((string) __('dberp.messages.product_sku_governance_cli_summary', [
            'dry' => $dryRun ? 'yes' : 'no',
            'report' => $result['report_path'] ?? '—',
        ]));

        $this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));

        return self::SUCCESS;
    }
}
