<?php

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

/**
 * Backup Schedule Tanımları.
 *
 * Birden fazla profil tanımlanabilir: "günlük tam", "haftalık DB only", vs.
 * Her profil bir destination'a yazar.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('backup_schedules', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->boolean('is_active')->default(true);

            // Cron expression
            $table->string('cron', 50)->default('0 4 * * *')
                ->comment('Standart 5-field cron');

            // What to back up
            $table->enum('scope', ['full', 'home_only', 'db_only', 'config_only'])
                ->default('full');
            $table->json('include_accounts')->nullable()
                ->comment('null = tüm hesaplar; array = sadece bu account_id\'ler');
            $table->json('exclude_paths')->nullable()
                ->comment('node_modules, tmp/, cache/ vd.');

            // Destination
            $table->foreignId('destination_id')
                ->constrained('backup_destinations')
                ->cascadeOnUpdate()
                ->restrictOnDelete();

            // Compression + encryption
            $table->enum('compression', ['none', 'gzip', 'zstd', 'xz'])->default('gzip');
            $table->boolean('encrypt')->default(false);
            $table->string('encryption_key_id', 100)->nullable();

            // Retention policy
            $table->unsignedInteger('keep_daily')->default(7);
            $table->unsignedInteger('keep_weekly')->default(4);
            $table->unsignedInteger('keep_monthly')->default(6);
            $table->unsignedInteger('keep_yearly')->default(2);

            // Last run
            $table->timestamp('last_run_at')->nullable();
            $table->enum('last_run_status', ['success', 'partial', 'failed'])->nullable();
            $table->text('last_run_error')->nullable();
            $table->unsignedInteger('last_run_duration_seconds')->nullable();
            $table->bigInteger('last_run_size_bytes')->nullable();

            // Next run (cached)
            $table->timestamp('next_run_at')->nullable();

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

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