<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\ValueObjects;

use App\Framework\Core\ValueObjects\Byte;
use InvalidArgumentException;

/**
 * Chunk Metadata
 *
 * Metadata für einen einzelnen Upload-Chunk.
 * Enthält Index, Größe und Hash für Integrität.
 */
final readonly class ChunkMetadata
{
    public function __construct(
        public int $index,           // 0-based chunk index
        public Byte $size,           // Chunk size
        public ChunkHash $hash,      // SHA-256 hash of chunk data
        public bool $uploaded = false // Upload status
    ) {
        if ($index < 0) {
            throw new InvalidArgumentException('Chunk index must be non-negative');
        }

        if ($size->isEmpty()) {
            throw new InvalidArgumentException('Chunk size must be greater than zero');
        }
    }

    public static function create(
        int $index,
        Byte $size,
        ChunkHash $hash
    ): self {
        return new self($index, $size, $hash);
    }

    public function withUploaded(): self
    {
        return new self(
            $this->index,
            $this->size,
            $this->hash,
            true
        );
    }

    public function isUploaded(): bool
    {
        return $this->uploaded;
    }

    public function toArray(): array
    {
        return [
            'index' => $this->index,
            'size' => $this->size->toBytes(),
            'size_human' => $this->size->toHumanReadable(),
            'hash' => $this->hash->toString(),
            'uploaded' => $this->uploaded,
        ];
    }
}
