<?php

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('api_agent_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('website_id')->constrained('managed_sites')->cascadeOnDelete();
            $table->foreignId('agent_id')->nullable()->constrained()->nullOnDelete();
            $table->string('direction')->default('outbound');
            $table->string('method', 10);
            $table->string('endpoint');
            $table->unsignedSmallInteger('http_status')->nullable();
            $table->unsignedInteger('latency_ms')->nullable();
            $table->boolean('was_successful')->default(false);
            $table->text('message')->nullable();
            $table->json('request_headers')->nullable();
            $table->json('request_payload')->nullable();
            $table->json('response_headers')->nullable();
            $table->json('response_body')->nullable();
            $table->timestamp('logged_at');
            $table->timestamps();

            $table->index(['website_id', 'logged_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('api_agent_logs');
    }
};
