<?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('dokter_pasien', function (Blueprint $table) {
            $table->id();
            $table->foreignId('dokter_id')->constrained('dokter')->onDelete('cascade');
            $table->foreignId('pasien_id')->constrained('pasien')->onDelete('cascade');
            
            // Informasi Penanganan
            $table->date('tanggal_mulai_menangani');
            $table->date('tanggal_selesai_menangani')->nullable();
            $table->enum('status_penanganan', [
                'AKTIF',
                'SELESAI',
                'DIRUJUK',
                'DIBATALKAN'
            ])->default('AKTIF');
            
            // Data Medis
            $table->text('diagnosis_utama')->nullable();
            $table->text('rencana_perawatan')->nullable();
            $table->integer('jumlah_kunjungan')->default(0);
            $table->date('tanggal_kontrol_berikutnya')->nullable();
            $table->text('catatan')->nullable();
            
            // Metadata
            $table->foreignId('didaftarkan_oleh')->nullable()->constrained('users')->nullOnDelete();
            
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('dokter_id');
            $table->index('pasien_id');
            $table->index('status_penanganan');
            $table->index('tanggal_kontrol_berikutnya');
            $table->unique(['dokter_id', 'pasien_id', 'status_penanganan'], 'unique_active_treatment');
        });
    }

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