<?php

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

/**
 * Backup Destinations.
 *
 * Yedeklerin yazılacağı hedef sistemler. Local + S3 + Restic + SFTP gibi.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('backup_destinations', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->enum('driver', ['local', 's3', 'restic', 'sftp', 'backblaze_b2', 'wasabi'])
                ->default('local');
            $table->boolean('is_active')->default(true);
            $table->boolean('is_default')->default(false);

            // S3 / B2 / Wasabi config
            $table->string('bucket', 100)->nullable();
            $table->string('region', 50)->nullable();
            $table->string('endpoint', 191)->nullable()
                ->comment('S3 compatible için custom endpoint');
            $table->string('access_key', 191)->nullable();
            $table->text('secret_key')->nullable()
                ->comment('Encrypted');
            $table->string('path_prefix', 191)->nullable();

            // Local / SFTP / Restic
            $table->string('local_path', 191)->nullable();
            $table->string('sftp_host', 191)->nullable();
            $table->unsignedSmallInteger('sftp_port')->nullable();
            $table->string('sftp_user', 100)->nullable();
            $table->text('sftp_private_key')->nullable();

            // Restic
            $table->string('restic_repo', 191)->nullable();
            $table->text('restic_password')->nullable();

            // Stats
            $table->bigInteger('total_bytes')->default(0)
                ->comment('Bu destination\'da toplam yedek boyutu');
            $table->integer('backup_count')->default(0);
            $table->timestamp('last_backup_at')->nullable();
            $table->timestamp('last_health_check_at')->nullable();
            $table->boolean('is_healthy')->default(true);
            $table->text('last_error')->nullable();

            $table->timestamps();
            $table->softDeletes();

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

    public function down(): void
    {
        Schema::dropIfExists('backup_destinations');
    }
};
