<?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('tenant_notification_receipts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->unsignedBigInteger('notification_log_id');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->string('recipient')->nullable();
            $table->enum('status', ['delivered', 'read', 'dismissed'])->default('delivered');
            $table->timestamps();

            $table->index(['tenant_id', 'notification_log_id']);
            $table->index(['tenant_id', 'user_id']);
            $table->index(['tenant_id', 'status']);
            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->foreign('notification_log_id')->references('id')->on('tenant_notification_logs')->cascadeOnDelete();
            $table->foreign('user_id')->references('id')->on('users')->nullOnDelete();
        });
    }

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