<?php

namespace App\ValueObjects;

use Carbon\Carbon;

class DOB extends Carbon {
    /**
     * DOB with age e.g. "27/12/01 (24)"
     */
    public function withAge(string $format = 'd/m/y'): string {
        return $this->formatted($format) . ' (' . $this->age . ' years old)';
    }

    /**
     * Default DOB display format.
     */
    public function formatted(string $format = 'd/m/y'): string {
        return $this->format($format);
    }

    /**
     * Safe age (never negative).
     */
    public function safeAge(): int {
        return max(0, $this->age);
    }

    /**
     * Age at a specific date.
     */
    public function ageAt(?Carbon $date = null): int {
        return $this->diffInYears($date ?? now());
    }
}