<?php

namespace App\Modules\Procurement\Console;

use App\Modules\Admin\Tenancy\Services\TenantContext;
use App\Modules\Procurement\Services\GoodsReceiptIntegrityAuditService;
use Illuminate\Console\Command;

class AuditReceiptIntegrityCommand extends Command
{
    protected $signature = 'inventory:audit-receipt-integrity {tenant : Tenant ID}';

    protected $description = 'Audit goods receipt lines for cross-PO, deleted PO items, and orphans';

    public function handle(TenantContext $ctx, GoodsReceiptIntegrityAuditService $audit): int
    {
        $tenantId = (int) $this->argument('tenant');
        $ctx->setTenantId($tenantId);

        $r = $audit->auditTenant($tenantId);
        $this->line('Severity: '.$r['severity']);
        $this->line('Active cross-PO lines: '.$r['active_cross_po']);
        $this->line('Active deleted-PO-item lines: '.$r['active_deleted_poi']);
        $this->line('Active missing-PO-item lines: '.$r['active_missing_poi']);
        $this->line('Quarantined lines (total): '.$r['quarantined_lines']);
        $this->line('Affected receipts: '.implode(',', $r['affected_receipt_ids']));
        $this->line('Affected line ids: '.implode(',', $r['affected_line_ids']));

        return self::SUCCESS;
    }
}
