<?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('log_notifikasi', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pemeriksaan_kesehatan_id')->nullable()->constrained('pemeriksaan_kesehatan')->nullOnDelete();
            $table->foreignId('pasien_id')->constrained('pasien')->onDelete('cascade');
            $table->foreignId('analisis_ai_id')->nullable()->constrained('analisis_ai')->nullOnDelete();
            
            // Informasi Notifikasi
            $table->enum('jenis_notifikasi', [
                'WHATSAPP',
                'SMS',
                'EMAIL',
                'PUSH_NOTIFICATION',
                'TELEGRAM'
            ])->default('WHATSAPP');
            
            $table->string('nomor_tujuan', 20)->nullable()->comment('Nomor WA/HP tujuan');
            $table->string('email_tujuan', 100)->nullable();
            
            // Isi Pesan
            $table->text('judul_pesan')->nullable();
            $table->longText('isi_pesan')->comment('Konten lengkap notifikasi');
            $table->enum('template_digunakan', [
                'HASIL_PEMERIKSAAN',
                'DEMAM_TINGGI',
                'PERINGATAN_KESEHATAN',
                'REMINDER_KONTROL',
                'CUSTOM'
            ])->nullable();
            
            // Status Pengiriman
            $table->enum('status_pengiriman', [
                'PENDING',
                'TERKIRIM',
                'GAGAL',
                'DIBATALKAN'
            ])->default('PENDING');
            
            $table->timestamp('waktu_dikirim')->nullable();
            $table->timestamp('waktu_diterima')->nullable()->comment('Jika ada konfirmasi delivery');
            $table->integer('retry_count')->default(0);
            $table->text('pesan_error')->nullable();
            
            // Metadata Pengiriman (untuk WhatsApp API dll)
            $table->string('message_id')->nullable()->comment('ID dari provider (WA Business API)');
            $table->string('provider', 50)->nullable()->comment('Fonnte, Twilio, WA Business, dll');
            $table->json('response_provider')->nullable()->comment('Response dari API provider');
            $table->decimal('biaya_pengiriman', 10, 4)->nullable()->comment('Biaya dalam Rupiah');
            
            // Interaksi User
            $table->boolean('dibaca')->default(false);
            $table->timestamp('waktu_dibaca')->nullable();
            $table->boolean('dibalas')->default(false);
            $table->text('balasan_user')->nullable();
            
            // Prioritas & Kategori
            $table->enum('prioritas', ['RENDAH', 'NORMAL', 'TINGGI', 'DARURAT'])->default('NORMAL');
            $table->enum('kategori', [
                'INFORMASI',
                'PERINGATAN',
                'DARURAT',
                'REMINDER',
                'PROMOSI'
            ])->default('INFORMASI');
            
            // Tracking
            $table->foreignId('dikirim_oleh')->nullable()->constrained('users')->nullOnDelete();
            $table->string('ip_address', 45)->nullable();
            $table->boolean('otomatis')->default(true)->comment('Otomatis dari sistem atau manual');
            
            // Metadata
            $table->json('metadata_tambahan')->nullable();
            $table->text('catatan')->nullable();
            
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('pemeriksaan_kesehatan_id');
            $table->index('pasien_id');
            $table->index('jenis_notifikasi');
            $table->index('status_pengiriman');
            $table->index('waktu_dikirim');
            $table->index('prioritas');
            $table->index(['pasien_id', 'waktu_dikirim']);
            $table->index('nomor_tujuan');
        });
    }

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