<?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('notifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('association_id')->constrained('associations')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            
            $table->string('type');
            $table->enum('channel', ['database', 'email', 'sms', 'all'])->default('database');
            
            $table->string('title');
            $table->text('body');
            
            $table->morphs('related');
            
            $table->timestamp('read_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            
            $table->text('failure_reason')->nullable();
            $table->json('metadata')->nullable();
            
            $table->timestamps();
            
            $table->index(['association_id', 'user_id', 'read_at']);
            $table->index(['user_id', 'read_at']);
            $table->index(['type', 'created_at']);
        });
    }

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