<?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('transactions', function (Blueprint $table) {
            // Core fields
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->string('reference')->unique();
            $table->decimal('amount', 16, 2);
            $table->string('currency', 3)->default('EUR');
            $table->string('counterparty');
            $table->string('counterparty_country', 2)->nullable();
            $table->string('channel')->nullable();
            $table->dateTime('executed_at');
            $table->unsignedTinyInteger('risk_score')->default(0);
            $table->string('status', 32)->index();
            $table->boolean('requires_review')->default(true);
            $table->string('flagged_by')->nullable();
            $table->text('flagged_reason')->nullable();
            $table->json('signals')->nullable();

            // Registry & source related fields (optional enrichment data)
            $table->text('registry_company_number')->nullable();
            $table->text('registry_source')->nullable();
            $table->double('registry_match_score')->nullable();
            $table->jsonb('registry_data')->nullable();
            $table->dateTime('registry_last_refreshed_at')->nullable();

            // Genesis context
            $table->jsonb('genesis_context')->nullable();
            $table->dateTime('genesis_last_refreshed_at')->nullable();

            // Govdata context
            $table->jsonb('govdata_data')->nullable();
            $table->dateTime('govdata_last_refreshed_at')->nullable();

            // Bundesanzeiger context
            $table->jsonb('bundesanzeiger_data')->nullable();
            $table->dateTime('bundesanzeiger_last_refreshed_at')->nullable();

            // Insolvency context
            $table->jsonb('insolvency_data')->nullable();
            $table->dateTime('insolvency_last_refreshed_at')->nullable();

            // RSS alerts
            $table->jsonb('rss_alerts')->nullable();
            $table->dateTime('rss_last_refreshed_at')->nullable();

            // Sanctions context
            $table->jsonb('sanctions_data')->nullable();
            $table->dateTime('sanctions_last_refreshed_at')->nullable();

            // PEP context
            $table->jsonb('pep_data')->nullable();
            $table->dateTime('pep_last_refreshed_at')->nullable();

            // GLEIF context
            $table->text('gleif_lei')->nullable();
            $table->json('gleif_data')->nullable();
            $table->dateTime('gleif_last_refreshed_at')->nullable();

            // EU sanctions context
            $table->jsonb('eu_sanctions_data')->nullable();
            $table->dateTime('eu_sanctions_last_refreshed_at')->nullable();

            // Handelsregister context
            $table->jsonb('handelsregister_data')->nullable();
            $table->dateTime('handelsregister_last_refreshed_at')->nullable();
            $table->text('handelsregister_status')->nullable();
            $table->bigInteger('handelsregister_entity_id')->nullable();

            // Timestamps
            $table->timestamps();
        });
    }

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