<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Credentiale Cloudflare - per utilizator sau globale (admin)
        Schema::create('cloudflare_credentials', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade'); // null = global admin
            $table->string('name')->default('Default'); // Nume pentru identificare
            $table->text('api_token'); // Token API (encrypted)
            $table->string('account_id')->nullable(); // ID cont Cloudflare
            $table->string('email')->nullable(); // Email cont Cloudflare
            $table->boolean('is_active')->default(true);
            $table->boolean('is_verified')->default(false); // Verificat prin API
            $table->timestamp('verified_at')->nullable();
            $table->timestamp('last_used_at')->nullable();
            $table->json('permissions')->nullable(); // Permisiuni detectate
            $table->timestamps();

            $table->index(['user_id', 'is_active']);
        });

        // Zone Cloudflare - legatura intre zonele locale si cele din Cloudflare
        Schema::create('cloudflare_zones', function (Blueprint $table) {
            $table->id();
            $table->foreignId('dns_zone_id')->nullable()->constrained('dns_zones')->onDelete('cascade');
            $table->foreignId('cloudflare_credential_id')->constrained('cloudflare_credentials')->onDelete('cascade');
            $table->string('cloudflare_zone_id', 32); // ID zona in Cloudflare
            $table->string('domain'); // Domeniu
            $table->enum('sync_direction', ['none', 'to_cloudflare', 'from_cloudflare', 'bidirectional'])->default('none');
            $table->boolean('auto_sync')->default(false); // Sincronizare automata
            $table->boolean('proxy_default')->default(false); // Proxy implicit pentru records noi
            $table->enum('status', ['active', 'pending', 'paused', 'deleted'])->default('pending');
            $table->string('cloudflare_status')->nullable(); // Status din Cloudflare API
            $table->json('name_servers')->nullable(); // NS-uri Cloudflare
            $table->json('original_name_servers')->nullable(); // NS-uri originale
            $table->timestamp('last_synced_at')->nullable();
            $table->text('last_sync_error')->nullable();
            $table->json('settings')->nullable(); // SSL, minify, etc.
            $table->timestamps();

            $table->unique(['cloudflare_zone_id', 'cloudflare_credential_id'], 'cf_zone_credential_unique');
            $table->index(['dns_zone_id']);
            $table->index(['domain']);
            $table->index('status');
        });

        // Setari per record pentru Cloudflare (proxy toggle, etc.)
        Schema::create('cloudflare_record_settings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('dns_record_id')->constrained('dns_records')->onDelete('cascade');
            $table->foreignId('cloudflare_zone_id')->constrained('cloudflare_zones')->onDelete('cascade');
            $table->string('cloudflare_record_id', 32)->nullable(); // ID record in Cloudflare
            $table->boolean('proxied')->default(false); // Proxy activ (portocaliu)
            $table->boolean('sync_enabled')->default(true); // Sincronizare activa
            $table->timestamp('last_synced_at')->nullable();
            $table->json('cloudflare_data')->nullable(); // Date suplimentare din CF
            $table->timestamps();

            $table->unique(['dns_record_id', 'cloudflare_zone_id'], 'cf_record_zone_unique');
            $table->index('cloudflare_record_id', 'cf_record_id_index');
        });

        // Log sincronizari Cloudflare
        Schema::create('cloudflare_sync_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('cloudflare_zone_id')->constrained('cloudflare_zones')->onDelete('cascade');
            $table->enum('direction', ['to_cloudflare', 'from_cloudflare']);
            $table->enum('action', ['create', 'update', 'delete', 'full_sync']);
            $table->enum('status', ['success', 'failed', 'partial']);
            $table->integer('records_processed')->default(0);
            $table->integer('records_success')->default(0);
            $table->integer('records_failed')->default(0);
            $table->text('error_message')->nullable();
            $table->json('details')->nullable();
            $table->integer('duration_ms')->nullable();
            $table->timestamps();

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('cloudflare_sync_logs');
        Schema::dropIfExists('cloudflare_record_settings');
        Schema::dropIfExists('cloudflare_zones');
        Schema::dropIfExists('cloudflare_credentials');
    }
};
