<?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_announcements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->uuid('uuid')->unique();
            $table->string('title', 200);
            $table->text('body');
            $table->string('target_type', 30);
            $table->jsonb('target_ids')->default('[]');
            $table->jsonb('push_data')->default('{}');
            $table->string('status', 20)->default('pending');
            $table->integer('sent_count')->default(0);
            $table->integer('failed_count')->default(0);
            $table->timestamp('scheduled_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();

            $table->index(['academy_id', 'status']);
            $table->index(['academy_id', 'scheduled_at']);
        });

        DB::statement("ALTER TABLE push_announcements ADD CONSTRAINT push_announcements_target_type_check CHECK (target_type IN ('all', 'group', 'branch', 'program'))");
        DB::statement("ALTER TABLE push_announcements ADD CONSTRAINT push_announcements_status_check CHECK (status IN ('pending', 'scheduled', 'sending', 'sent', 'failed'))");
    }

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