<?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('platform_operators', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('role', 32)->default('super_admin');
            $table->boolean('is_active')->default(true);
            $table->string('mfa_secret')->nullable();
            $table->boolean('mfa_enabled')->default(false);
            $table->timestamp('last_login_at')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });

        Schema::create('platform_audit_logs', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->foreignUuid('operator_id')->nullable()->constrained('platform_operators')->nullOnDelete();
            $table->string('action', 120);
            $table->string('subject_type', 120)->nullable();
            $table->uuid('subject_id')->nullable();
            $table->uuid('tenant_id')->nullable()->index();
            $table->json('payload')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamps();

            $table->index(['action', 'created_at']);
        });

        Schema::create('platform_settings', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->string('key')->unique();
            $table->json('value')->nullable();
            $table->timestamps();
        });

        Schema::create('platform_connectors', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->string('code', 64)->unique();
            $table->string('name');
            $table->string('category', 64);
            $table->string('status', 32)->default('unknown');
            $table->json('meta')->nullable();
            $table->timestamp('last_checked_at')->nullable();
            $table->timestamps();
        });

        Schema::create('platform_health_snapshots', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->string('overall_status', 32);
            $table->json('checks');
            $table->timestamps();
        });

        Schema::create('platform_support_actions', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->foreignUuid('operator_id')->constrained('platform_operators')->cascadeOnDelete();
            $table->uuid('tenant_id')->nullable()->index();
            $table->string('action_type', 64);
            $table->text('notes')->nullable();
            $table->json('payload')->nullable();
            $table->timestamps();
        });

        Schema::create('platform_impersonation_logs', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->foreignUuid('operator_id')->constrained('platform_operators')->cascadeOnDelete();
            $table->uuid('tenant_id')->index();
            $table->uuid('user_id')->index();
            $table->text('justification');
            $table->timestamp('started_at');
            $table->timestamp('ends_at');
            $table->timestamp('ended_at')->nullable();
            $table->timestamps();
        });

        Schema::create('platform_incidents', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('severity', 16)->default('medium');
            $table->string('status', 32)->default('open');
            $table->json('affected_tenant_ids')->nullable();
            $table->foreignUuid('opened_by')->nullable()->constrained('platform_operators')->nullOnDelete();
            $table->foreignUuid('closed_by')->nullable()->constrained('platform_operators')->nullOnDelete();
            $table->timestamp('resolved_at')->nullable();
            $table->timestamp('closed_at')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('platform_incidents');
        Schema::dropIfExists('platform_impersonation_logs');
        Schema::dropIfExists('platform_support_actions');
        Schema::dropIfExists('platform_health_snapshots');
        Schema::dropIfExists('platform_connectors');
        Schema::dropIfExists('platform_settings');
        Schema::dropIfExists('platform_audit_logs');
        Schema::dropIfExists('platform_operators');
    }
};
