<?php

namespace App\Modules\Inventory\Stock\Console;

use App\Modules\Admin\Tenancy\Services\TenantContext;
use App\Modules\Inventory\Stock\Services\InventoryStockRebuildService;
use Illuminate\Console\Command;

class InventoryRebuildProjectionsCommand extends Command
{
    protected $signature = 'inventory:rebuild-projections {tenant : Tenant ID}';

    protected $description = 'Rebuild inventory_stock_projections from ledger (maintenance)';

    public function handle(TenantContext $ctx, InventoryStockRebuildService $rebuild): int
    {
        $tenantId = (int) $this->argument('tenant');
        $ctx->setTenantId($tenantId);
        $res = $rebuild->rebuildTenant($tenantId);
        $this->info('Projection rows: '.$res['rows'].', drift before rebuild: '.($res['mismatched_before'] ? 'yes' : 'no'));

        return self::SUCCESS;
    }
}
