<?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('coupon_redemptions', function (Blueprint $table) {
            $table->string('ip_address', 45)->nullable()->after('failure_reason');
            $table->string('device_id', 64)->nullable()->after('ip_address');
            $table->text('user_agent')->nullable()->after('device_id');

            $table->index(['ip_address', 'created_at'], 'coupon_redemptions_ip_time_idx');
            $table->index(['device_id', 'created_at'], 'coupon_redemptions_device_time_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('coupon_redemptions', function (Blueprint $table) {
            $table->dropIndex('coupon_redemptions_ip_time_idx');
            $table->dropIndex('coupon_redemptions_device_time_idx');
            $table->dropColumn(['user_agent', 'device_id', 'ip_address']);
        });
    }
};
