<?php

namespace Fotogenic_AI;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

final class Studio_Cache_Store {
    private const CACHE_GROUP = 'fotogenic_studio';
    private const TRANSIENT_PREFIX = 'fotogenic_ai_studio_';

    public static function bump_latest_products_cache_version(): void {
        $version = self::get_latest_products_cache_version();
        self::cache_set( 'latest_products_cache_version', $version + 1, 0 );
    }

    public static function bump_products_query_cache_version(): void {
        $version = self::get_products_query_cache_version();
        self::cache_set( 'products_query_cache_version', $version + 1, 0 );
    }

    public static function bump_product_summary_cache_version(): void {
        $version = self::get_product_summary_cache_version();
        self::cache_set( 'product_summary_cache_version', $version + 1, 0 );
    }

    public static function bump_product_context_cache_version(): void {
        $version = self::get_product_context_cache_version();
        self::cache_set( 'product_context_cache_version', $version + 1, 0 );
    }

    public static function flush_all_caches(): void {
        // Summary/context keys are user-scoped. Version bumps invalidate every
        // authorized user's cached projection without enumerating user IDs.
        self::bump_latest_products_cache_version();
        self::bump_products_query_cache_version();
        self::bump_product_summary_cache_version();
        self::bump_product_context_cache_version();
    }

    public static function get_latest_products_cache_version(): int {
        $version = self::cache_get( 'latest_products_cache_version' );
        if ( ! is_numeric( $version ) || (int) $version <= 0 ) {
            $version = 1;
            self::cache_set( 'latest_products_cache_version', $version, 0 );
        }

        return (int) $version;
    }

    public static function get_products_query_cache_version(): int {
        $version = self::cache_get( 'products_query_cache_version' );
        if ( ! is_numeric( $version ) || (int) $version <= 0 ) {
            $version = 1;
            self::cache_set( 'products_query_cache_version', $version, 0 );
        }

        return (int) $version;
    }

    public static function get_product_summary_cache_version(): int {
        $version = self::cache_get( 'product_summary_cache_version' );
        if ( ! is_numeric( $version ) || (int) $version <= 0 ) {
            $version = 1;
            self::cache_set( 'product_summary_cache_version', $version, 0 );
        }

        return (int) $version;
    }

    public static function get_product_context_cache_version(): int {
        $version = self::cache_get( 'product_context_cache_version' );
        if ( ! is_numeric( $version ) || (int) $version <= 0 ) {
            $version = 1;
            self::cache_set( 'product_context_cache_version', $version, 0 );
        }

        return (int) $version;
    }

    private static function cache_get( string $key ) {
        $value = wp_cache_get( $key, self::CACHE_GROUP );
        if ( false !== $value ) {
            return $value;
        }

        $transient_key = self::TRANSIENT_PREFIX . str_replace( ':', '_', $key );
        $value         = get_transient( $transient_key );

        return false === $value ? false : $value;
    }

    private static function cache_set( string $key, $value, int $ttl_seconds ): void {
        wp_cache_set( $key, $value, self::CACHE_GROUP, $ttl_seconds );

        $transient_key = self::TRANSIENT_PREFIX . str_replace( ':', '_', $key );
        $transient_ttl = $ttl_seconds > 0 ? $ttl_seconds : 10 * YEAR_IN_SECONDS;
        set_transient( $transient_key, $value, $transient_ttl );
    }

}
