<?php

declare(strict_types=1);

namespace App\Framework\Discovery\Cache;

use App\Framework\Cache\CacheKey;
use App\Framework\Cache\CacheTag;
use App\Framework\Discovery\ValueObjects\ScanType;

/**
 * Centralized cache identifiers for discovery operations
 * Uses the framework's CacheIdentifier system for consistent caching
 */
final readonly class DiscoveryCacheIdentifiers
{
    /**
     * Create cache tag for all discovery-related cache items
     */
    public static function discoveryTag(): CacheTag
    {
        return CacheTag::forType('discovery');
    }

    /**
     * Create cache key for discovery results based on paths, scan type, and execution context
     * @param string[] $paths
     */
    public static function discoveryKey(array $paths, ScanType $scanType, ?string $context = null): CacheKey
    {
        $pathsHash = md5(implode('|', $paths));
        $contextSuffix = $context ? "_{$context}" : '';
        $keyString = "discovery:{$scanType->value}_{$pathsHash}{$contextSuffix}";

        return CacheKey::fromString($keyString);
    }

    /**
     * Create cache key for full discovery
     * @param string[] $paths
     */
    public static function fullDiscoveryKey(array $paths, ?string $context = null): CacheKey
    {
        return self::discoveryKey($paths, ScanType::FULL, $context);
    }

    /**
     * Create cache key for incremental discovery
     * @param string[] $paths
     */
    public static function incrementalDiscoveryKey(array $paths, ?string $context = null): CacheKey
    {
        return self::discoveryKey($paths, ScanType::INCREMENTAL, $context);
    }

    /**
     * Cache key for lazy components that are loaded on-demand
     */
    public static function lazyComponentsKey(): CacheKey
    {
        return CacheKey::fromString('discovery:lazy_components');
    }

    /**
     * Cache key for directory last modified timestamps (used for cache validation)
     */
    public static function lastModifiedKey(string $path): CacheKey
    {
        $pathHash = md5($path);

        return CacheKey::fromString("discovery:lastmod_{$pathHash}");
    }
}
