<?php

declare(strict_types=1);

namespace App\Framework\Vault\ValueObjects;

use DateTimeImmutable;

/**
 * Metadata für Vault Secrets (Audit Trail)
 */
final readonly class VaultMetadata
{
    public function __construct(
        public SecretKey $key,
        public DateTimeImmutable $createdAt,
        public DateTimeImmutable $updatedAt,
        public ?string $createdBy = null,
        public ?string $updatedBy = null,
        public int $accessCount = 0,
        public ?DateTimeImmutable $lastAccessedAt = null
    ) {
    }

    public function withAccess(DateTimeImmutable $accessedAt): self
    {
        return new self(
            key: $this->key,
            createdAt: $this->createdAt,
            updatedAt: $this->updatedAt,
            createdBy: $this->createdBy,
            updatedBy: $this->updatedBy,
            accessCount: $this->accessCount + 1,
            lastAccessedAt: $accessedAt
        );
    }

    public function withUpdate(DateTimeImmutable $updatedAt, ?string $updatedBy = null): self
    {
        return new self(
            key: $this->key,
            createdAt: $this->createdAt,
            updatedAt: $updatedAt,
            createdBy: $this->createdBy,
            updatedBy: $updatedBy ?? $this->updatedBy,
            accessCount: $this->accessCount,
            lastAccessedAt: $this->lastAccessedAt
        );
    }
}
