<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('webhook_test_sessions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('name', 100);
            $table->string('endpoint_url', 500);
            $table->enum('method', ['POST', 'PUT', 'PATCH', 'DELETE'])->default('POST');
            $table->json('headers')->nullable();
            $table->longText('body')->nullable();
            $table->string('hmac_secret', 200)->nullable();
            $table->string('hmac_header_name', 100)->default('X-Onox-Signature');
            $table->json('last_response')->nullable();
            $table->integer('last_status_code')->nullable();
            $table->integer('last_duration_ms')->nullable();
            $table->timestamp('last_sent_at')->nullable();
            $table->timestamps();
        });

        Schema::create('api_test_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('method', 10);
            $table->string('path', 500);
            $table->json('params')->nullable();
            $table->json('headers')->nullable();
            $table->longText('body')->nullable();
            $table->integer('status_code')->nullable();
            $table->longText('response_body')->nullable();
            $table->integer('duration_ms')->nullable();
            $table->timestamp('sent_at')->useCurrent();
            $table->timestamps();
            $table->index(['user_id', 'sent_at']);
        });

        Schema::create('webhook_receivers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('slug', 32)->unique();
            $table->string('name', 100);
            $table->boolean('is_active')->default(true);
            $table->integer('max_payload_kb')->default(64);
            $table->timestamps();
        });

        Schema::create('webhook_received_messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('webhook_receiver_id')->constrained()->cascadeOnDelete();
            $table->string('method', 10);
            $table->json('headers');
            $table->json('query_params')->nullable();
            $table->longText('body')->nullable();
            $table->string('content_type', 100)->nullable();
            $table->string('remote_ip', 45);
            $table->string('user_agent', 500)->nullable();
            $table->timestamp('received_at')->useCurrent();
            $table->timestamps();
            $table->index(['webhook_receiver_id', 'received_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('webhook_received_messages');
        Schema::dropIfExists('webhook_receivers');
        Schema::dropIfExists('api_test_requests');
        Schema::dropIfExists('webhook_test_sessions');
    }
};
