<?php

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

return new class extends Migration
{
    /**
     * Per-inventory-row lot/tracking code, format SS-PPP-UUU:
     *   SS  = first two letters of the supplier's display name
     *   PPP = this product's sequence number for that supplier (assigned
     *         once per supplier+product pair, reused on later purchases)
     *   UUU = running count of individual rows (pieces/boxes) ever
     *         recorded for that exact supplier+product pair
     *
     * Generated by PurchaseProduct::generateLotCode() inside the same
     * transaction as the row's save() — see PurchaseService::syncLines().
     * Nullable, no backfill: only rows created after this migration get
     * a code; historical rows show blank.
     *
     * No unique constraint: the 2-letter supplier code alone can collide
     * across two suppliers that share their first two letters — a known
     * limitation of the requested format, not enforced at the DB level.
     */
    public function up(): void
    {
        Schema::table('purchase_products', function (Blueprint $table) {
            $table->string('lot_code', 30)->nullable()->after('barcode')->index();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('purchase_products', function (Blueprint $table) {
            $table->dropColumn('lot_code');
        });
    }
};
