<?php
if(!defined('ABSPATH')){
    exit;
}

if (!function_exists('db')) {
    function db($some, $pass = false) {
        echo '<pre/>';
        var_dump($some);
        if($pass){
            exit;
        }
    }
}

if (!function_exists('format_lang_stage')) {
    function format_lang_stage($stages) {
        if (empty($stages) || !is_array($stages)) {
            return [];
        }

        $locale = substr(get_user_locale(), 0, 2);

        $result = [];

        foreach ($stages as $stage) {
            $label = isset($stage[$locale]) ? $stage[$locale] : ($stage['en'] ?? '');

            $result[] = [
                'id' => $stage['id'],
                'label' => $label
            ];
        }

        return $result;
    }
}

if(!function_exists('buildPropName')){
    function buildPropName($type, $stage, $town, $lang = 'en')
    {
        $type = rtrim($type, 's'); 
        $translatedType = $labels['prop_type'][$type][$lang] ?? $type;

        $preposition = $labels['preposition'][$lang] ?? 'in';

        return sprintf('%s %s %s %s', 
            $translatedType,
            strtolower($stage),
            $preposition,
            $town
        );
    }

}
if(!function_exists('T_')){
    function T_($element){
        return  __($element, 'new-devs.v2');
    }
}

if(!function_exists('get_all_custom_routes')){
    function get_all_custom_routes_by_lang() {
        $langs = ['es','en','de','da'];
        $routes = [];

        foreach ($langs as $lang) {
            $route = get_option("zoddak_route_{$lang}", '');
            if ($route) {
                $routes[$lang] = trim($route, '/');
            }
        }

        return $routes;   
    }


}

if(!function_exists('get_route_dict')){
    function get_route_dict(){
        $langs = ['es','en', 'de', 'da'];
        $dict = [];

        foreach($langs as $lang){
            $value = get_option("zoddak_route_$lang", '');
            if(!empty($value)){
                $dict[$lang] = $value;
            }
        }
        return $dict;
    }
}
if(!function_exists('access_denied_message')){
    function access_denied_message(){
        return '<div class="zoddak-access-denied" style="
                max-width: 600px;
                margin: 60px auto;
                padding: 30px;
                background: #fafafa;
                border: 1px solid #ddd;
                border-radius: 6px;
                text-align: center;
                font-family: sans-serif;
            ">
                <h2 style="
                    margin-top: 0;
                    font-size: 24px;
                    color: #333;
                    font-weight: 600;
                ">' . T_('Access denied') . '</h2>

                <p style="
                    font-size: 16px;
                    color: #555;
                    margin-bottom: 0;
                ">' . T_('This section is only available to administrators.') . '</p>
            </div>';
    }
}

if(!function_exists('zoddak_get_api_base_url')){
    function zoddak_get_api_base_url(){
        $host = $_SERVER['HTTP_HOST'] ?? '';
        error_log('Current host: ' . $host);
        if(strpos($host, 'wordpress.local') !== false || strpos($host, 'localhost') !== false){
            return 'http://zoddak-beta.test/api';
        }
        return 'https://app.zoddak.com/api';
    }
}

if(!function_exists('do_not_cache')){
    function do_not_cache()
    {
        define('DONOTCACHEPAGE', true);
		header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
		header('Pragma: no-cache');
    }
}