<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('branch_stock', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained('warehouses')->cascadeOnUpdate()->cascadeOnDelete();

            // product_id has no FK yet — products table is built in Phase 2.
            // Index on (branch_id, product_id) UNIQUE enforces one stock row per (branch, product).
            $table->unsignedBigInteger('product_id');

            $table->decimal('quantity', 12, 4)->default(0);
            $table->timestamps();

            $table->unique(['branch_id', 'product_id']);
            $table->index('product_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('branch_stock');
    }
};
