<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\ValueObjects;

use App\Framework\Core\ValueObjects\Hash;
use App\Framework\Core\ValueObjects\HashAlgorithm;

/**
 * Chunk Integrity Hash
 *
 * SHA-256 Hash für Chunk-Integrität.
 * Wrapper um Framework's Hash VO mit Upload-spezifischer Semantik.
 */
final readonly class ChunkHash
{
    private function __construct(
        private Hash $hash
    ) {
    }

    /**
     * Calculate hash from chunk data
     */
    public static function fromData(string $data): self
    {
        return new self(Hash::sha256($data));
    }

    /**
     * Create from existing hash string
     */
    public static function fromString(string $hash): self
    {
        return new self(Hash::fromString($hash, HashAlgorithm::SHA256));
    }

    /**
     * Calculate hash from file
     */
    public static function fromFile(string $filepath): self
    {
        return new self(Hash::fromFile($filepath, HashAlgorithm::SHA256));
    }

    /**
     * Verify chunk data matches this hash
     */
    public function verify(string $data): bool
    {
        return $this->hash->verify($data);
    }

    /**
     * Verify file matches this hash
     */
    public function verifyFile(string $filepath): bool
    {
        return $this->hash->verifyFile($filepath);
    }

    public function equals(self $other): bool
    {
        return $this->hash->equals($other->hash);
    }

    public function toString(): string
    {
        return $this->hash->toString();
    }

    public function __toString(): string
    {
        return $this->hash->toString();
    }

    /**
     * Get underlying Hash value object
     */
    public function getHash(): Hash
    {
        return $this->hash;
    }
}
