<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('dkim_keys', function (Blueprint $table) {
            $table->id();
            $table->string('domain', 253);
            $table->string('selector', 100)->default('onox2026');
            $table->integer('key_size')->default(2048);
            $table->text('public_key');
            $table->text('private_key');
            $table->boolean('is_active')->default(true);
            $table->timestamp('rotated_at')->nullable();
            $table->integer('rotation_overlap_days')->default(7);
            $table->enum('algorithm', ['rsa-sha256', 'ed25519-sha256'])->default('rsa-sha256');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['domain', 'selector']);
            $table->index('domain');
        });
    }

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