<?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
    {
        if (! Schema::hasTable('dw_notifications')) {
            Schema::create('dw_notifications', function (Blueprint $table) {
                $table->id();
                $table->foreignId('client_id')->nullable()->constrained('clients')->nullOnDelete();
                $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
                $table->string('fingerprint', 191)->unique();
                $table->string('category', 60)->index();
                $table->string('source_type', 80)->nullable()->index();
                $table->unsignedBigInteger('source_id')->nullable()->index();
                $table->string('level', 30)->default('info')->index();
                $table->string('title');
                $table->text('message')->nullable();
                $table->string('action_label')->nullable();
                $table->text('action_url')->nullable();
                $table->json('payload')->nullable();
                $table->timestamp('read_at')->nullable()->index();
                $table->timestamp('dismissed_at')->nullable()->index();
                $table->timestamp('expires_at')->nullable()->index();
                $table->timestamps();

                $table->index(['client_id', 'level', 'read_at']);
                $table->index(['category', 'created_at']);
            });
        }
    }

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