<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('stock_codes', function (Blueprint $table) {
            $table->string('code_fingerprint', 128)->nullable()->after('encrypted_code');
            $table->foreignId('imported_by')->nullable()->after('currency_code')->constrained('users')->nullOnDelete();
            $table->text('import_note')->nullable()->after('imported_by');
            $table->timestamp('voided_at')->nullable()->after('sold_at');
            $table->unique(['product_variant_id', 'code_fingerprint']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('stock_codes', function (Blueprint $table) {
            $table->dropUnique(['product_variant_id', 'code_fingerprint']);
            $table->dropConstrainedForeignId('imported_by');
            $table->dropColumn(['code_fingerprint', 'import_note', 'voided_at']);
        });
    }
};
