<?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('care_blood_bank_stock', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('care_branches')->nullOnDelete();
            $table->string('product_code', 64);
            $table->string('product_label');
            $table->unsignedInteger('units_on_hand')->default(0);
            $table->unsignedInteger('reorder_level')->default(2);
            $table->text('notes')->nullable();
            $table->string('updated_by')->nullable();
            $table->timestamps();

            $table->unique(
                ['organization_id', 'branch_id', 'product_code'],
                'care_bb_stock_org_branch_product_unique',
            );
            $table->index(['organization_id', 'branch_id']);
        });
    }

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