<?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('units', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);        // "Kilogram", "Lembar", "Meter"
            $table->string('symbol', 20);       // "kg", "lbr", "m"
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // Add unit_id to products table
        Schema::table('products', function (Blueprint $table) {
            $table->foreignId('unit_id')->nullable()->after('description')->constrained('units')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->dropForeign(['unit_id']);
            $table->dropColumn('unit_id');
        });

        Schema::dropIfExists('units');
    }
};
