<?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('order_fraud_checks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');

            // Check details
            $table->string('check_type'); // velocity, ip_reputation, device, email, billing_address, amount, etc.
            $table->string('check_status'); // passed, failed, warning, skipped
            $table->integer('risk_score')->default(0); // 0-100
            $table->text('reason')->nullable(); // Why it failed/warned

            // Check data
            $table->json('check_data')->nullable(); // Detailed check information
            $table->json('metadata')->nullable(); // Additional metadata

            // Timing
            $table->integer('execution_time_ms')->nullable(); // Check duration
            $table->timestamp('checked_at');
            $table->timestamps();

            // Indexes
            $table->index('order_id');
            $table->index(['order_id', 'check_type']);
            $table->index('check_status');
            $table->index('risk_score');
            $table->index('checked_at');
        });
    }

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