<?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::table('users', function (Blueprint $table) {
            $table->foreignId('student_id')->nullable()->after('staff_id')->constrained('students')->nullOnDelete();
        });

        Schema::table('messages', function (Blueprint $table) {
            $table->string('audience_type')->default('individual')->after('priority')->index();
            $table->json('audience_targets')->nullable()->after('audience_type');
        });

        Schema::create('notifications', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('type');
            $table->morphs('notifiable');
            $table->text('data');
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });
    }

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

        Schema::table('messages', function (Blueprint $table) {
            $table->dropColumn(['audience_type', 'audience_targets']);
        });

        Schema::table('users', function (Blueprint $table) {
            $table->dropConstrainedForeignId('student_id');
        });
    }
};
