<?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('email_accounts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->restrictOnDelete();
            $table->string('local', 64);
            $table->string('domain', 253);
            $table->string('password_hash');
            $table->bigInteger('quota_mb')->default(1024);
            $table->bigInteger('usage_bytes')->default(0);
            $table->enum('status', ['active', 'suspended', 'pending'])->default('pending');
            $table->boolean('auto_create_dirs')->default(true);
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['local', 'domain']);
            $table->index(['account_id', 'status']);
        });

        Schema::create('email_forwarders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->restrictOnDelete();
            $table->string('source');
            $table->text('destinations');
            $table->boolean('keep_local_copy')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
            $table->index('account_id');
        });

        Schema::create('email_autoresponders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('email_account_id')->constrained('email_accounts')->cascadeOnDelete();
            $table->string('subject');
            $table->text('body');
            $table->string('from_name')->nullable();
            $table->timestamp('start_at')->nullable();
            $table->timestamp('stop_at')->nullable();
            $table->unsignedInteger('interval_hours')->default(24);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('email_autoresponders');
        Schema::dropIfExists('email_forwarders');
        Schema::dropIfExists('email_accounts');
    }
};
