<?php
namespace PageVICO\Cache;

defined( 'ABSPATH' ) || exit;

class CacheManager {
	const CACHE_GROUP         = 'pagevico';
	const CACHE_DURATION      = 15 * MINUTE_IN_SECONDS;
	const LONG_CACHE_DURATION = 24 * HOUR_IN_SECONDS;

	/**
	 * Get cached data
	 */
	public static function get( $key ) {
		return wp_cache_get( $key, self::CACHE_GROUP );
	}

	/**
	 * Set cached data
	 */
	public static function set( $key, $data, $duration = null ) {
		$duration = $duration ?? self::CACHE_DURATION;
		return wp_cache_set( $key, $data, self::CACHE_GROUP, $duration );
	}

	/**
	 * Delete cached data
	 */
	public static function delete( $key ) {
		return wp_cache_delete( $key, self::CACHE_GROUP );
	}

	/**
	 * Clear all plugin caches
	 */
	public static function clearAll() {
		wp_cache_flush_group( self::CACHE_GROUP );
	}

	/**
	 * Clear caches when new pageview is recorded
	 */
	public static function clearOnNewPageview() {
		self::clearAll();
	}

	/**
	 * Clear caches when new session is created
	 */
	public static function clearOnNewSession() {
		self::clearAll();
	}

	/**
	 * Generate cache key for date-based queries
	 */
	public static function generateDateKey( $prefix, $date_from, $date_to, $paged = 1 ) {
		return "{$prefix}_{$date_from}_{$date_to}_{$paged}";
	}

	/**
	 * Generate cache key for time-based queries
	 */
	public static function generateTimeKey( $prefix ) {
		return "{$prefix}_" . current_time( 'Y-m-d_H' );
	}
}
