<?php

declare(strict_types=1);

namespace App\Framework\View\Caching\Strategies;

use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheKey;
use App\Framework\View\Caching\TemplateContext;

class FragmentCacheStrategy implements ViewCacheStrategy
{
    public function __construct(private Cache $cache)
    {
    }

    public function shouldCache(TemplateContext $context): bool
    {
        return isset($context->metadata['fragment_id']);
    }

    public function generateKey(TemplateContext $context): CacheKey
    {
        $fragment = $context->metadata['fragment_id'] ?? 'default';

        return CacheKey::fromString("fragment:{$context->template}:{$fragment}:" . md5(serialize($context->data)));
    }

    public function getTtl(TemplateContext $context): int
    {
        return 600; // 10 minutes - Fragments kürzer cachen
    }

    public function canInvalidate(string $template): bool
    {
        return true;
    }
}
