<?php

namespace App\Console\Commands;

use App\Services\LocationInventoryService;
use App\Services\LocationService;
use Illuminate\Console\Command;

class BackfillLocationInventory extends Command
{
    protected $signature = 'inventory:backfill-locations {--location= : Location ID to backfill into}';

    protected $description = 'Backfill inventory_balances from products.current_stock for SKUs with no balances (default: central warehouse; UAT/prod use inventory:multistore-cutover for store-first)';

    public function handle(LocationInventoryService $inventory, LocationService $locations): int
    {
        $locations->ensureCompanyAndDefaults();
        $locationId = $this->option('location') ? (int) $this->option('location') : null;
        $count = $inventory->backfillFromProductStock($locationId);
        $this->info("Backfilled {$count} product balance rows.");

        return self::SUCCESS;
    }
}
