<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;

class CacheService
{
    // Cache TTL constants (in seconds)
    public const TTL_SHORT = 30;
    public const TTL_MEDIUM = 60;
    public const TTL_LONG = 300;
    public const TTL_EXTENDED = 900;
    public const TTL_ONLINE = 60;

    // Cache key prefixes
    public const PREFIX_DASHBOARD = 'dashboard:';
    public const PREFIX_ADMIN = 'admin:';
    public const PREFIX_STATS = 'stats:';
    public const PREFIX_ONLINE = 'online:';

    public static function remember(string $key, int $ttl, callable $callback): mixed
    {
        return Cache::remember($key, $ttl, $callback);
    }

    public static function forget(string $key): bool
    {
        return Cache::forget($key);
    }

    public static function forgetByPrefix(string $prefix): void
    {
        // Note: This only works reliably with Redis/Memcached
        // For database/file cache, consider using tags instead
        Cache::flush();
    }

    public static function dashboardKey(string $key): string
    {
        return self::PREFIX_DASHBOARD . $key;
    }

    public static function adminKey(string $key): string
    {
        return self::PREFIX_ADMIN . $key;
    }

    public static function statsKey(string $key): string
    {
        return self::PREFIX_STATS . $key;
    }

    public static function onlineKey(string $key): string
    {
        return self::PREFIX_ONLINE . $key;
    }

    public static function invalidateDashboardCache(): void
    {
        Cache::forget(self::dashboardKey('stats'));
        Cache::forget(self::dashboardKey('online_list'));
        Cache::forget(self::adminKey('stats'));
        Cache::forget(self::adminKey('recent_users'));
    }
}
