<?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('backup_schedules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained('managed_environments')->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_enabled')->default(true);
            $table->string('timezone')->default('UTC');
            $table->enum('frequency', ['daily', 'weekly']);
            $table->time('time_of_day');
            $table->json('days_of_week')->nullable();
            $table->json('policy_types');
            $table->boolean('include_foundations')->default(true);
            $table->integer('retention_keep_last')->default(30);
            $table->dateTime('last_run_at')->nullable();
            $table->string('last_run_status')->nullable();
            $table->dateTime('next_run_at')->nullable();
            $table->timestamps();

            $table->index(['managed_environment_id', 'is_enabled']);
            $table->index('next_run_at');
        });
    }

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