<?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('archive_settings', function (Blueprint $table): void {
            $table->id();

            // Contextual foreign keys (nullable allows broader global rules)
            $table->foreignId('action_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('decision_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('segment_id')->nullable()->constrained()->nullOnDelete();

            // JSON describing entities (tables/models) impacted
            // Example shape: [{"table":"documents","conditions":{"older_than_days":180},"columns":["id","deleted_at"]}]
            $table->json('entities');

            // Optional descriptive metadata
            $table->string('name')->nullable();
            $table->text('description')->nullable();
            $table->boolean('enabled')->default(true);

            // Execution strategy: immediate | scheduled | queued
            $table->string('strategy')->default('immediate');

            // Whether to perform a soft archive (flag / soft delete) instead of permanent removal
            $table->boolean('soft')->default(true);

            // Additional arbitrary options (thresholds, flags, custom logic parameters)
            $table->json('options')->nullable();

            // Auditing foreign keys for who created / last updated the rule
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();

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

            // Useful indexes
            $table->index(['action_id', 'decision_id', 'segment_id']);
            $table->index('enabled');
            $table->index('strategy');
        });
    }

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