<?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('embedding_wajah', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pasien_id')->constrained('pasien')->onDelete('cascade');
            $table->string('id_eksternal')->comment('ID eksternal pasien untuk sinkronisasi');
            
            // Data Embedding
            $table->longText('vektor_embedding')->comment('Face embedding dalam format JSON array');
            $table->integer('dimensi_vektor')->default(128)->comment('Jumlah dimensi vector (128/512)');
            $table->string('model_encoding', 50)->default('dlib')->comment('Model: dlib, facenet, arcface');
            $table->string('versi_model', 20)->nullable()->comment('Versi model yang digunakan');
            
            // Kualitas & Validasi
            $table->decimal('skor_kepercayaan', 5, 2)->nullable()->comment('Confidence score 0-100');
            $table->enum('kualitas_gambar', ['SANGAT_BAIK', 'BAIK', 'CUKUP', 'BURUK'])->default('BAIK');
            $table->integer('jumlah_wajah_terdeteksi')->default(1)->comment('Jumlah wajah dalam gambar');
            $table->boolean('validasi_passed')->default(true)->comment('Lolos validasi wajah');
            $table->text('catatan_validasi')->nullable();
            
            // Metadata Gambar (tanpa menyimpan file asli)
            $table->string('resolusi_gambar', 20)->nullable()->comment('Contoh: 640x480');
            $table->json('koordinat_wajah')->nullable()->comment('Posisi wajah: {x, y, width, height}');
            $table->decimal('ukuran_gambar_kb', 8, 2)->nullable()->comment('Ukuran file dalam KB');
            
            // Status & Tracking
            $table->enum('status_embedding', ['AKTIF', 'NONAKTIF', 'PERLU_UPDATE'])->default('AKTIF');
            $table->timestamp('tanggal_registrasi')->useCurrent();
            $table->timestamp('tanggal_update_terakhir')->nullable();
            $table->foreignId('didaftarkan_oleh')->constrained('users')->onDelete('cascade');
            $table->string('ip_address', 45)->nullable();
            $table->string('user_agent')->nullable();
            
            // Riwayat Pengenalan
            $table->integer('total_dikenali')->default(0)->comment('Berapa kali wajah berhasil dikenali');
            $table->timestamp('terakhir_dikenali')->nullable();
            
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('pasien_id');
            $table->index('id_eksternal');
            $table->index('status_embedding');
            $table->index('tanggal_registrasi');
            $table->unique(['pasien_id', 'status_embedding'], 'unique_active_embedding');
        });
    }

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