<?php

declare(strict_types=1);

namespace App\Framework\Process\ValueObjects\Backup;

use App\Framework\Core\ValueObjects\Byte;
use App\Framework\Core\ValueObjects\Duration;
use App\Framework\Filesystem\ValueObjects\FilePath;

/**
 * Backup-Datei Information.
 */
final readonly class BackupFile
{
    public function __construct(
        public FilePath $path,
        public Byte $size,
        public \DateTimeImmutable $createdAt,
        public string $name
    ) {
    }

    /**
     * Gibt das Alter des Backups zurück.
     */
    public function getAge(): Duration
    {
        $now = new \DateTimeImmutable();
        $diff = $now->getTimestamp() - $this->createdAt->getTimestamp();

        return Duration::fromSeconds($diff);
    }

    /**
     * Prüft, ob das Backup älter als X Tage ist.
     */
    public function isOlderThan(Duration $duration): bool
    {
        return $this->getAge()->greaterThan($duration);
    }

    /**
     * Prüft, ob das Backup frisch ist (< 24 Stunden).
     */
    public function isFresh(): bool
    {
        return ! $this->isOlderThan(Duration::fromDays(1));
    }

    /**
     * Konvertiert zu Array.
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'path' => $this->path->toString(),
            'size_bytes' => $this->size->toBytes(),
            'size_human' => $this->size->toHumanReadable(),
            'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
            'age_human' => $this->getAge()->toHumanReadable(),
            'is_fresh' => $this->isFresh(),
        ];
    }
}
