<?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('orders', function (Blueprint $table) {
            $table->foreignId('coupon_id')->nullable()->after('user_id')->constrained()->nullOnDelete();
            $table->string('coupon_code')->nullable()->after('coupon_id');
            $table->json('discount_snapshot')->nullable()->after('risk_snapshot');
            $table->unsignedBigInteger('cashback_minor')->default(0)->after('discount_minor');
            $table->unsignedInteger('loyalty_points_earned')->default(0)->after('cashback_minor');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropConstrainedForeignId('coupon_id');
            $table->dropColumn(['coupon_code', 'discount_snapshot', 'cashback_minor', 'loyalty_points_earned']);
        });
    }
};
