<?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('pemeriksaan_kesehatan', function (Blueprint $table) {
            $table->id();
            $table->string('kode_pemeriksaan', 30)->unique()->comment('Kode unik pemeriksaan: CHK-YYYYMMDD-XXXX');
            $table->foreignId('pasien_id')->constrained('pasien')->onDelete('cascade');
            $table->string('id_eksternal_pasien')->comment('ID eksternal pasien');
            
            // Identifikasi Wajah
            $table->foreignId('embedding_wajah_id')->nullable()->constrained('embedding_wajah')->nullOnDelete();
            $table->boolean('teridentifikasi_wajah')->default(false)->comment('Apakah wajah berhasil dikenali');
            $table->decimal('skor_pengenalan_wajah', 5, 2)->nullable()->comment('Similarity score 0-100');
            $table->text('catatan_pengenalan')->nullable();
            
            // Data Suhu Tubuh
            $table->decimal('suhu_tubuh', 4, 2)->comment('Suhu dalam Celcius');
            $table->enum('sumber_data', ['MANUAL', 'ESP32', 'SENSOR_LAIN'])->default('MANUAL');
            $table->string('mac_address_device', 20)->nullable()->comment('MAC address ESP32');
            
            // Klasifikasi Suhu
            $table->enum('kategori_suhu', [
                'HIPOTERMIA',        // < 35°C
                'NORMAL',            // 35-37.5°C
                'SUBFEBRIS',         // 37.5-38°C
                'DEMAM_RINGAN',      // 38-38.5°C
                'DEMAM_SEDANG',      // 38.5-39.5°C
                'DEMAM_TINGGI',      // > 39.5°C
            ])->nullable();
            
            $table->enum('status_kesehatan', ['SEHAT', 'PERLU_PERHATIAN', 'PERLU_PEMERIKSAAN_LANJUT'])->nullable();
            
            // Data Tambahan (opsional untuk pemeriksaan lengkap)
            $table->integer('detak_jantung')->nullable()->comment('BPM');
            $table->integer('tekanan_darah_sistol')->nullable()->comment('mmHg');
            $table->integer('tekanan_darah_diastol')->nullable()->comment('mmHg');
            $table->integer('saturasi_oksigen')->nullable()->comment('SpO2 %');
            $table->decimal('berat_badan_saat_ini', 5, 2)->nullable()->comment('kg');
            
            // Keluhan & Gejala
            $table->text('keluhan_utama')->nullable();
            $table->json('gejala_lain')->nullable()->comment('Array gejala: batuk, pilek, dll');
            $table->text('catatan_petugas')->nullable();
            
            // Hasil & Tindakan
            $table->boolean('analisis_ai_selesai')->default(false);
            $table->boolean('notifikasi_terkirim')->default(false);
            $table->text('tindakan_yang_dilakukan')->nullable();
            $table->text('rekomendasi')->nullable();
            
            // Tracking
            $table->timestamp('waktu_pemeriksaan')->useCurrent();
            $table->foreignId('diperiksa_oleh')->constrained('users')->onDelete('cascade');
            $table->string('lokasi_pemeriksaan', 100)->nullable()->comment('Ruangan/Poli');
            $table->enum('jenis_pemeriksaan', ['RUTIN', 'KELUHAN', 'KONTROL', 'DARURAT'])->default('RUTIN');
            $table->enum('status_pemeriksaan', ['SELESAI', 'DALAM_PROSES', 'DIBATALKAN'])->default('SELESAI');
            
            // Metadata
            $table->string('ip_address', 45)->nullable();
            $table->decimal('durasi_pemeriksaan', 8, 2)->nullable()->comment('Dalam detik');
            
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('kode_pemeriksaan');
            $table->index('pasien_id');
            $table->index('id_eksternal_pasien');
            $table->index('waktu_pemeriksaan');
            $table->index('kategori_suhu');
            $table->index('status_kesehatan');
            $table->index('sumber_data');
            $table->index('diperiksa_oleh');
            $table->index(['pasien_id', 'waktu_pemeriksaan']);
        });
    }

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