<?php

namespace Edoobox\Requests;

use Edoobox\Api\ShortcodeApi;
use Edoobox\Base\Settings;

class Page {

    public static function getCurrentPageCacheKey($attributes = null, bool $next = false): string {
        global $post;
        global $wp;

        $postId = $post->ID ?? 0;
        $auth = new Auth();
        $settings = new Settings();

        $attributes = $attributes ?? ($post ? ShortcodeApi::getAttributes($post) : []);

        // Erweiterung: bekannte GET-Parameter berücksichtigen
        $knownParams = ['edcode', 'edref', 'edtag', 'edpromo', 'edlanguage', 'edlink', 'edstart', 'edend', 'edview', 'q', 'edfilter'];
        foreach ($knownParams as $param) {
            if (isset($_GET[$param])) {
                $attributes[$param] = $_GET[$param];
            }
        }

        $implodedArray = implode(',', $attributes);
        $currentUrl = home_url(add_query_arg([], $wp->request));
        $apiVersion = $settings->get(Settings::USE_V2) ? 'V2' : 'V1';
        $nextFlag = $next ? '_next' : '';

        return md5(trim("{$postId}_{$apiVersion}_{$auth->getPublicKey()}_" . md5($currentUrl) . $implodedArray . $nextFlag));
    }


    public static function get_current_url(): string {
        return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    }

}