<?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('parts', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();

            $t->string('name');
            $t->string('article', 64)->nullable();
            $t->string('brand', 64)->nullable();
            $t->string('category')->nullable();          // Ulei / Filtre / Frâne / Lichide / ...

            $t->decimal('qty', 10, 2)->default(0);
            $t->string('unit', 16)->default('buc');
            $t->decimal('min_qty', 10, 2)->default(0);

            $t->decimal('buy_price', 10, 2)->default(0);
            $t->decimal('sell_price', 10, 2)->default(0);

            $t->string('location', 64)->nullable();      // ex "A1-M1" rack/bin
            $t->string('barcode', 64)->nullable();
            $t->foreignId('preferred_supplier_id')->nullable()->constrained('suppliers')->nullOnDelete();

            $t->boolean('is_active')->default(true);
            $t->text('notes')->nullable();

            $t->timestamps();
            $t->softDeletes();

            $t->index(['company_id', 'category']);
            $t->index(['company_id', 'is_active']);
            $t->unique(['company_id', 'article']);
        });
    }

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