<?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('ip_blocks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->string('ip_or_cidr', 50)->comment('192.168.1.1 veya 10.0.0.0/8');
            $table->string('comment')->nullable();
            $table->enum('mode', ['deny', 'allow'])->default('deny');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
            $table->index('account_id');
        });

        Schema::create('hotlink_protections', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->unique()->constrained('accounts')->cascadeOnDelete();
            $table->boolean('enabled')->default(false);
            $table->json('allowed_referrers')->nullable()->comment('Whitelist domains');
            $table->json('protected_extensions')->nullable()->comment('jpg, png, gif, mp4...');
            $table->string('redirect_url')->nullable()->comment('Engellenince yönlendir');
            $table->boolean('allow_direct')->default(true);
            $table->timestamps();
        });

        Schema::create('ssh_keys', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->string('label');
            $table->enum('key_type', ['ssh-rsa', 'ssh-ed25519', 'ssh-ecdsa', 'sk-ssh-ed25519'])->default('ssh-ed25519');
            $table->text('public_key');
            $table->string('fingerprint', 100)->nullable();
            $table->boolean('is_authorized')->default(true)->comment('authorized_keys\'e yazıldı mı');
            $table->timestamp('last_used_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index('account_id');
        });

        Schema::create('directory_passwords', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->string('path')->comment('Korunan yol: /admin');
            $table->string('username');
            $table->string('password_hash')->comment('htpasswd format (bcrypt veya crypt)');
            $table->string('realm', 100)->default('Restricted');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
            $table->index(['account_id', 'path']);
        });

        Schema::create('error_pages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->foreignId('domain_id')->nullable()->constrained('domains')->cascadeOnDelete();
            $table->unsignedSmallInteger('http_code')->comment('400, 401, 403, 404, 500');
            $table->text('html_content');
            $table->timestamps();
            $table->unique(['account_id', 'domain_id', 'http_code']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('error_pages');
        Schema::dropIfExists('directory_passwords');
        Schema::dropIfExists('ssh_keys');
        Schema::dropIfExists('hotlink_protections');
        Schema::dropIfExists('ip_blocks');
    }
};
