<?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
    {
        Schema::create('backups', function (Blueprint $table) {
            $table->id();
            
            // Tip și identificare
            $table->enum('type', ['full', 'incremental'])->default('full');
            $table->string('filename');
            $table->string('file_path', 500);
            $table->bigInteger('file_size')->default(0); // bytes
            
            // Conținut backup
            $table->boolean('includes_database')->default(true);
            $table->boolean('includes_files')->default(true);
            
            // Metadata
            $table->integer('tables_count')->nullable();
            $table->integer('records_count')->nullable();
            $table->integer('files_count')->nullable();
            
            // Backup incremental
            $table->foreignId('parent_backup_id')->nullable()->constrained('backups')->onDelete('set null');
            $table->timestamp('changes_since')->nullable();
            
            // Status
            $table->enum('status', ['pending', 'in_progress', 'completed', 'failed'])->default('pending');
            $table->text('error_message')->nullable();
            
            // Timing
            $table->timestamp('started_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->integer('duration_seconds')->nullable();
            
            // Utilizator
            $table->foreignId('created_by')->constrained('users')->onDelete('cascade');
            
            // Restore info
            $table->timestamp('restored_at')->nullable();
            $table->foreignId('restored_by')->nullable()->constrained('users')->onDelete('set null');
            
            // Criptare
            $table->boolean('is_encrypted')->default(false);
            
            // Destinații (JSON array cu ID-uri de destinații unde a fost uploadat)
            $table->json('uploaded_to')->nullable();
            
            $table->timestamps();
            
            // Indexuri
            $table->index('type');
            $table->index('status');
            $table->index('created_at');
        });
    }

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