<?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::dropIfExists('backup_schedule_runs');
        Schema::dropIfExists('entra_group_sync_runs');
        Schema::dropIfExists('inventory_sync_runs');
    }

    public function down(): void
    {
        if (! Schema::hasTable('inventory_sync_runs')) {
            Schema::create('inventory_sync_runs', function (Blueprint $table) {
                $table->id();
                $table->foreignId('managed_environment_id')->constrained();
                $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
                $table->string('selection_hash', 64);
                $table->jsonb('selection_payload')->nullable();
                $table->string('status');
                $table->boolean('had_errors')->default(false);
                $table->jsonb('error_codes')->nullable();
                $table->jsonb('error_context')->nullable();
                $table->timestampTz('started_at')->nullable();
                $table->timestampTz('finished_at')->nullable();
                $table->unsignedInteger('items_observed_count')->default(0);
                $table->unsignedInteger('items_upserted_count')->default(0);
                $table->unsignedInteger('errors_count')->default(0);
                $table->foreignId('operation_run_id')->nullable()->constrained('operation_runs')->nullOnDelete();
                $table->timestamps();

                $table->index(['managed_environment_id', 'selection_hash']);
                $table->index(['managed_environment_id', 'status']);
                $table->index(['managed_environment_id', 'finished_at']);
                $table->index(['managed_environment_id', 'user_id']);
                $table->index('operation_run_id');
            });
        }

        if (! Schema::hasTable('entra_group_sync_runs')) {
            Schema::create('entra_group_sync_runs', function (Blueprint $table) {
                $table->id();
                $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
                $table->string('selection_key');
                $table->string('slot_key')->nullable();
                $table->string('status');
                $table->string('error_code')->nullable();
                $table->string('error_category')->nullable();
                $table->text('error_summary')->nullable();
                $table->boolean('safety_stop_triggered')->default(false);
                $table->string('safety_stop_reason')->nullable();
                $table->unsignedInteger('pages_fetched')->default(0);
                $table->unsignedInteger('items_observed_count')->default(0);
                $table->unsignedInteger('items_upserted_count')->default(0);
                $table->unsignedInteger('error_count')->default(0);
                $table->foreignId('initiator_user_id')->nullable()->constrained('users')->nullOnDelete();
                $table->timestampTz('started_at')->nullable();
                $table->timestampTz('finished_at')->nullable();
                $table->foreignId('operation_run_id')->nullable()->constrained('operation_runs')->nullOnDelete();
                $table->timestamps();

                $table->index(['managed_environment_id', 'selection_key']);
                $table->index(['managed_environment_id', 'status']);
                $table->index(['managed_environment_id', 'finished_at']);
                $table->index('operation_run_id');
                $table->unique(['managed_environment_id', 'selection_key', 'slot_key']);
            });
        }

        if (! Schema::hasTable('backup_schedule_runs')) {
            Schema::create('backup_schedule_runs', function (Blueprint $table) {
                $table->id();
                $table->foreignId('backup_schedule_id')->constrained('backup_schedules')->cascadeOnDelete();
                $table->foreignId('managed_environment_id')->constrained('managed_environments')->cascadeOnDelete();
                $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
                $table->dateTime('scheduled_for');
                $table->dateTime('started_at')->nullable();
                $table->dateTime('finished_at')->nullable();
                $table->enum('status', ['running', 'success', 'partial', 'failed', 'canceled', 'skipped']);
                $table->json('summary')->nullable();
                $table->string('error_code')->nullable();
                $table->text('error_message')->nullable();
                $table->foreignId('backup_set_id')->nullable()->constrained('backup_sets')->nullOnDelete();
                $table->foreignId('operation_run_id')->nullable()->constrained('operation_runs')->nullOnDelete();
                $table->timestamps();

                $table->unique(['backup_schedule_id', 'scheduled_for']);
                $table->index(['backup_schedule_id', 'scheduled_for']);
                $table->index(['managed_environment_id', 'created_at']);
                $table->index(['user_id', 'created_at'], 'backup_schedule_runs_user_created');
                $table->index('operation_run_id');
            });
        }
    }
};
