<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('notification_templates', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->nullable()->constrained('academies')->nullOnDelete();
            $table->string('event_type', 100);
            $table->string('channel', 20);
            $table->string('subject')->nullable();
            $table->text('body');
            $table->jsonb('variables')->default('[]');
            $table->boolean('is_active')->default(true);
            $table->string('locale', 5)->default('ar');
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();

            $table->index(['academy_id', 'event_type', 'channel', 'locale'], 'notif_tpl_academy_event_channel_locale_idx');
            $table->index(['event_type', 'channel'], 'notif_tpl_event_channel_idx');
            $table->unique(['academy_id', 'event_type', 'channel', 'locale'], 'notif_tpl_unique_combo');
        });

        DB::statement("ALTER TABLE notification_templates ADD CONSTRAINT notification_templates_channel_check CHECK (channel IN ('in_app', 'email', 'sms'))");

        Schema::create('notification_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('recipient_type', 30);
            $table->unsignedBigInteger('recipient_id');
            $table->string('event_type', 100);
            $table->string('channel', 20);
            $table->string('subject')->nullable();
            $table->text('body');
            $table->string('status', 20);
            $table->text('error_message')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('read_at')->nullable();
            $table->jsonb('metadata')->default('{}');
            $table->timestamp('created_at')->nullable();

            $table->index(['academy_id', 'recipient_type', 'recipient_id', 'created_at'], 'notif_log_academy_recipient_created_idx');
            $table->index(['event_type', 'status'], 'notif_log_event_status_idx');
            $table->index(['channel', 'status', 'created_at'], 'notif_log_channel_status_created_idx');
        });

        DB::statement("ALTER TABLE notification_logs ADD CONSTRAINT notification_logs_channel_check CHECK (channel IN ('in_app', 'email', 'sms'))");
        DB::statement("ALTER TABLE notification_logs ADD CONSTRAINT notification_logs_status_check CHECK (status IN ('sent', 'failed', 'queued', 'read'))");

        Schema::create('notification_preferences', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('event_type', 100);
            $table->boolean('channel_email')->default(true);
            $table->boolean('channel_sms')->default(true);
            $table->boolean('channel_in_app')->default(true);
            $table->boolean('digest_mode')->default(false);
            $table->time('digest_time')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'event_type'], 'notif_pref_user_event_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('notification_preferences');
        Schema::dropIfExists('notification_logs');
        Schema::dropIfExists('notification_templates');
    }
};
