<?php

namespace Licenzo\Framework\Http;

/**
 * Header Bag for managing HTTP headers
 */
class HeaderBag
{
    /**
     * Header storage.
     */
    protected array $headers = [];

    /**
     * Create a new header bag.
     */
    public function __construct(array $headers = [])
    {
        $this->headers = $headers;
    }

    /**
     * Get all headers.
     */
    public function all(): array
    {
        return $this->headers;
    }

    /**
     * Get a header value.
     */
    public function get(string $key, $default = null)
    {
        return $this->headers[$key] ?? $default;
    }

    /**
     * Set a header value.
     */
    public function set(string $key, $value): void
    {
        $this->headers[$key] = $value;
    }

    /**
     * Check if a header exists.
     */
    public function has(string $key): bool
    {
        return array_key_exists($key, $this->headers);
    }

    /**
     * Remove a header.
     */
    public function remove(string $key): void
    {
        unset($this->headers[$key]);
    }

    /**
     * Replace all headers.
     */
    public function replace(array $headers): void
    {
        $this->headers = $headers;
    }

    /**
     * Add headers.
     */
    public function add(array $headers): void
    {
        $this->headers = array_merge($this->headers, $headers);
    }

    /**
     * Get header keys.
     */
    public function keys(): array
    {
        return array_keys($this->headers);
    }

    /**
     * Count headers.
     */
    public function count(): int
    {
        return count($this->headers);
    }
}
