<?php
/**
 * Hitung HPL (Hari Perkiraan Lahir) menggunakan Rumus Naegele
 * HPL = HPHT + 280 hari
 */
function hitungHPL(string $hpht): string {
    $tgl = new DateTime($hpht);
    $tgl->modify('+280 days');
    return $tgl->format('Y-m-d');
}

/**
 * Hitung usia kehamilan dalam minggu dan hari
 */
function hitungUsiaKehamilan(string $hpht): array {
    try {
        $hpht_date = new DateTime($hpht);
        $today     = new DateTime();
        $diff      = $hpht_date->diff($today);
        $total_hari = $diff->days;
        
        // Pastikan HPHT tidak di masa depan (jika diperlukan)
        if ($hpht_date > $today) {
            return ['minggu' => 0, 'hari' => 0, 'total_hari' => 0];
        }

        $minggu     = intdiv($total_hari, 7);
        $sisa_hari  = $total_hari % 7;
        return ['minggu' => $minggu, 'hari' => $sisa_hari, 'total_hari' => $total_hari];
    } catch (Exception $e) {
        return ['minggu' => 0, 'hari' => 0, 'total_hari' => 0];
    }
}

/**
 * Tentukan trimester berdasarkan usia kehamilan (minggu)
 */
function getTrimester(int $minggu): string {
    if ($minggu <= 13) return '1';
    if ($minggu <= 26) return '2';
    return '3';
}
