<?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('kylin_notification_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('notification_id')->nullable()->constrained('kylin_notifications')->nullOnDelete();
            $table->string('channel', 32)->comment('推送渠道: database/mail/aliyun_sms/tencent_sms/wechat_template/jpush/getui/umeng/fcm/apns');
            $table->string('status', 16)->default('pending')->comment('发送状态: pending/success/failed');
            $table->string('message_id')->nullable()->comment('渠道返回的消息ID');
            $table->string('error', 500)->nullable()->comment('错误信息');
            $table->json('request_params')->nullable()->comment('请求参数');
            $table->json('raw_response')->nullable()->comment('渠道原始响应');
            $table->timestamp('sent_at')->nullable()->comment('发送时间');
            $table->timestamps();

            $table->index(['notification_id', 'channel']);
            $table->index('channel');
            $table->index('status');
            $table->index('sent_at');
        });
    }

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