<?php

use App\Models\Setting;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('settings')) {
            return;
        }

        // After balances exist, reads prefer location inventory (LocationInventoryService).
        // products.current_stock remains a denormalized company aggregate updated on ledger writes.
        Setting::updateOrCreate(
            ['key' => 'inventory_location_mode'],
            ['value' => 'balances']
        );

        Setting::updateOrCreate(
            ['key' => 'inventory_legacy_movements_deprecated'],
            ['value' => '0']
        );
    }

    public function down(): void
    {
        if (!Schema::hasTable('settings')) {
            return;
        }

        Setting::whereIn('key', [
            'inventory_location_mode',
            'inventory_legacy_movements_deprecated',
        ])->delete();
    }
};
