<?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('push_analytics', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('user_id')->constrained('users');
            $table->string('notification_id', 100)->nullable();
            $table->string('event_type', 50);
            $table->string('action', 20);
            $table->jsonb('metadata')->default('{}');
            $table->timestamp('created_at');

            $table->index(['academy_id', 'event_type', 'created_at']);
            $table->index(['user_id', 'created_at']);
        });

        DB::statement("ALTER TABLE push_analytics ADD CONSTRAINT push_analytics_action_check CHECK (action IN ('delivered', 'opened', 'dismissed', 'action_clicked'))");
    }

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