<?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('admin_messages', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->longText('body');
            $table->string('status', 30)->default('draft'); // draft, sent
            $table->string('channel', 30)->nullable(); // sms, email, internal
            $table->json('recipient_filters')->nullable(); // filters used to select users
            $table->foreignId('project_id')->nullable()->constrained('projects')->nullOnDelete();
            $table->timestamp('sent_at')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('admin_message_recipients', function (Blueprint $table) {
            $table->id();
            $table->foreignId('admin_message_id')->constrained('admin_messages')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('channel', 30)->nullable();
            $table->string('status', 30)->default('pending'); // pending, sent, read, failed
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });
    }

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