<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('fcm_send_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('user_id')->nullable()->comment('대상 사용자');
            $table->foreignId('sender_id')->nullable()->comment('발송자 (MNG 관리자)');
            $table->string('title');
            $table->text('body');
            $table->string('channel_id', 50)->default('push_default');
            $table->string('type', 50)->nullable()->comment('알림 타입');
            $table->string('platform', 20)->nullable()->comment('대상 플랫폼');
            $table->json('data')->nullable()->comment('추가 데이터');
            $table->unsignedInteger('total_count')->default(0);
            $table->unsignedInteger('success_count')->default(0);
            $table->unsignedInteger('failure_count')->default(0);
            $table->unsignedInteger('invalid_token_count')->default(0);
            $table->decimal('success_rate', 5, 2)->default(0);
            $table->string('status', 20)->default('pending')->comment('pending, sending, completed, failed');
            $table->text('error_message')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'created_at']);
            $table->index(['status', 'created_at']);
            $table->index('sender_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('fcm_send_logs');
    }
};
