<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\Services;

use App\Framework\LiveComponents\ValueObjects\ChunkHash;

/**
 * Integrity Validator
 *
 * Validiert Chunk- und File-Integrität via SHA-256 Hashes.
 * Stellt sicher dass Uploads nicht korrumpiert wurden.
 */
final readonly class IntegrityValidator
{
    /**
     * Verify chunk data matches provided hash
     *
     * @param string $chunkData Raw chunk data
     * @param ChunkHash $expectedHash Expected chunk hash
     * @return bool True if hash matches
     */
    public function verifyChunk(string $chunkData, ChunkHash $expectedHash): bool
    {
        return $expectedHash->verify($chunkData);
    }

    /**
     * Verify file matches provided hash
     *
     * @param string $filePath Path to file
     * @param ChunkHash $expectedHash Expected file hash
     * @return bool True if hash matches
     */
    public function verifyFile(string $filePath, ChunkHash $expectedHash): bool
    {
        if (!file_exists($filePath)) {
            return false;
        }

        return $expectedHash->verifyFile($filePath);
    }

    /**
     * Calculate hash for chunk data
     *
     * @param string $chunkData Raw chunk data
     * @return ChunkHash Calculated hash
     */
    public function calculateChunkHash(string $chunkData): ChunkHash
    {
        return ChunkHash::fromData($chunkData);
    }

    /**
     * Calculate hash for file
     *
     * @param string $filePath Path to file
     * @return ChunkHash Calculated hash
     */
    public function calculateFileHash(string $filePath): ChunkHash
    {
        return ChunkHash::fromFile($filePath);
    }
}
