<?php

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

/**
 * Migration: Create meeting_logs table
 * 
 * This table logs all Google Meet API interactions for debugging, auditing,
 * and monitoring purposes. It tracks meeting creation, test connections,
 * and token refresh operations.
 * 
 * @see https://developers.google.com/calendar/api/v3/reference/events
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('meeting_logs', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Log Type and Status
            $table->enum('type', [
                'create_meeting',   // Meeting creation attempt
                'test',             // Connection test
                'token_refresh',    // OAuth2 token refresh
                'revoke_access'     // Token revocation
            ]);
            $table->enum('status', ['success', 'failed']);

            // Meeting Details
            $table->string('summary', 191)->nullable();     // Meeting title/summary
            $table->integer('attendees_count')->nullable(); // Number of attendees

            // Response Data
            $table->string('meeting_link', 500)->nullable(); // Google Meet URL
            $table->string('meeting_id', 191)->nullable();   // Google Meet ID
            $table->json('response_body')->nullable();       // Full API response (JSON)

            // Error Handling
            $table->text('error_message')->nullable();       // Error description if failed

            // Performance Metrics
            $table->integer('execution_time_ms')->default(0); // Execution time in milliseconds

            // Timestamps
            $table->timestamp('created_at')->useCurrent();

            // Indexes for performance
            $table->index('type');
            $table->index('status');
            $table->index('created_at');
            $table->index(['type', 'status']);
            $table->index(['status', 'created_at']);
        });
    }

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