<?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('guest_notifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('event_id')->constrained()->cascadeOnDelete();
            $table->string('type', 64);
            $table->string('title', 160);
            $table->text('body')->nullable();
            $table->json('payload')->nullable();
            $table->string('audience_scope', 32)->default('all');
            $table->string('target_identifier', 120)->nullable();
            $table->string('status', 32)->default('active');
            $table->unsignedTinyInteger('priority')->default(0);
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();

            $table->index(['event_id', 'audience_scope']);
            $table->index(['event_id', 'status']);
            $table->index(['event_id', 'created_at']);
        });

        Schema::create('guest_notification_receipts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('guest_notification_id')->constrained()->cascadeOnDelete();
            $table->string('guest_identifier', 120);
            $table->string('status', 32)->default('new');
            $table->timestamp('read_at')->nullable();
            $table->timestamp('dismissed_at')->nullable();
            $table->timestamps();

            $table->unique(['guest_notification_id', 'guest_identifier'], 'guest_notifications_guest_identifier_unique');
            $table->index(['guest_identifier', 'status']);
        });
    }

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