<?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::create('stock_codes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_variant_id')->constrained()->cascadeOnDelete();
            $table->string('batch_reference')->nullable();
            $table->text('encrypted_code');
            $table->string('serial_number')->nullable()->index();
            $table->unsignedBigInteger('cost_minor')->default(0);
            $table->char('currency_code', 3)->default('EGP');
            $table->date('expires_at')->nullable();
            $table->string('status')->default('available')->index();
            $table->foreignId('reserved_order_id')->nullable()->constrained('orders')->nullOnDelete();
            $table->foreignId('sold_order_item_id')->nullable()->constrained('order_items')->nullOnDelete();
            $table->timestamp('reserved_at')->nullable();
            $table->timestamp('sold_at')->nullable();
            $table->unique(['product_variant_id', 'serial_number']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('stock_codes');
    }
};
