<?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('barcode_labels', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->nullable()->index();
            $table->ulid('uuid')->unique();
            // The scannable value. Deliberately its own namespace (LBL-…) so a
            // scan can be told apart from a product SKU/manufacturer barcode
            // without a database round-trip, and so the two can never collide.
            $table->string('code', 50);
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            // The label is printed for stock standing in one branch: its lot and
            // its serial both belong to that branch, and the scan resolves
            // against it.
            // `warehouses` is the legacy table name the UI labels "Branch" (D3).
            $table->foreignId('branch_id')->nullable()->constrained('warehouses')->cascadeOnDelete();
            // Required for a batch-tracked product — the lot IS the price, so a
            // label that skipped it would still open the lot picker on scan,
            // which is the whole thing this feature removes.
            $table->foreignId('stock_batch_id')->nullable()->constrained('stock_batches')->cascadeOnDelete();
            // Required for a serial-tracked product. One physical item, one
            // label: a serial can never carry two.
            $table->foreignId('product_serial_id')->nullable()->unique()->constrained('product_serials')->cascadeOnDelete();
            // What one scan adds, as keyed in by the operator, in `unit_id`.
            $table->decimal('quantity', 12, 4)->default(1);
            $table->foreignId('unit_id')->nullable()->constrained('unit_of_measures')->restrictOnDelete();
            // The same amount converted to the product's base unit — the figure
            // the terminal actually puts on the cart line, so a "1 BOX" label
            // and a "250 g" label both land as plain base-unit quantities.
            $table->decimal('base_quantity', 12, 4)->default(1);
            // Optional caption printed above the barcode instead of the product
            // name (shelf-edge wording, a shorter till-facing name).
            $table->string('label_name', 150)->nullable();
            $table->string('note', 500)->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('printed_count')->default(0);
            $table->timestamp('last_printed_at')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

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

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