<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Business\Organization\OrganizationMasterDataBackfillService;
use Illuminate\Console\Command;

final class DberpOrganizationMasterDataScanCommand extends Command
{
    protected $signature = 'dberp:organization-master-data-scan
                            {--apply : 执行安全回填（默认仅扫描，不写库）}
                            {--report : 生成 JSON 治理报告文件}';

    protected $description = '扫描职员/业务员历史部门与岗位文本：唯一匹配启用主档时可回填；多匹配或无匹配写入治理报告；关联职员时可确定性补齐业务员外键';

    public function handle(OrganizationMasterDataBackfillService $service): int
    {
        $dryRun = ! (bool) $this->option('apply');
        $writeReport = (bool) $this->option('report');

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

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

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

        return self::SUCCESS;
    }
}
