<?php

defined( 'ABSPATH' ) || exit;

use Vendtics\WpMVC\App;
use Vendtics\DI\Container;
use Vendtics\App\DateTime;
use Vendtics\App\Services\DynamicVariable;
use Vendtics\App\Repositories\ContactRepository;
use Vendtics\App\Repositories\SegmentRepository;
use Vendtics\App\Repositories\CampaignRepository;
use Vendtics\App\Repositories\SettingsRepository;

function vendtics():App {
    return App::$instance;
}

function vendtics_config( string $config_key ) {
    return vendtics()::$config->get( $config_key );
}

function vendtics_app_config( string $config_key ) {
    return vendtics_config( "app.{$config_key}" );
}

function vendtics_version() {
    return vendtics_app_config( 'version' );
}

function vendtics_container():Container {
    return vendtics()::$container;
}

/**
 * Get a singleton instance of a class from the container.
 *
 * @template T
 * @param class-string<T> $class The class name.
 * @return T The instance of the class.
 */
function vendtics_singleton( string $class ) {
    return vendtics_container()->get( $class );
}

function vendtics_url( string $url = '' ) {
    return vendtics()->get_url( $url );
}

function vendtics_dir( string $dir = '' ) {
    return vendtics()->get_dir( $dir );
}

function vendtics_render( string $content ) {
    //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo $content;
}

function vendtics_is_one_level_array( array $array ) {
    foreach ( $array as $value ) {
        if ( is_array( $value ) ) {
            return false;
        }
    }
    return true; // Valid one-level array
}

function vendtics_date_time_format() {
    return apply_filters( 'vendtics_date_time_format', 'Y-m-d H:i:s' );
}

function vendtics_now() {
    return DateTime::now();
}

function vendtics_contact_repository(): ContactRepository {
    return vendtics_singleton( ContactRepository::class );
}

function vendtics_campaign_repository(): CampaignRepository {
    return vendtics_singleton( CampaignRepository::class );
}

function vendtics_segment_repository(): SegmentRepository {
    return vendtics_singleton( SegmentRepository::class );
}

function vendtics_settings_repository(): SettingsRepository {
    return vendtics_singleton( SettingsRepository::class );
}

function vendtics_anchor_url_regex() {
    return '/<a\s+[^>]*href=["\']([^"\']+)["\']/i';
}

function vendtics_email_add_utm_params( string $content, ?string $utm_params ) {
    if ( empty( $utm_params ) ) {
        return $content;
    }

    $utm_params = json_decode( $utm_params, true );

    return preg_replace_callback(
        vendtics_anchor_url_regex(), function ( $matches ) use ( $utm_params ) {
            $original_url = $matches[1];

            $updated_url = add_query_arg( $utm_params, $original_url );

            // Return the updated <a> tag
            return str_replace( $original_url, $updated_url, $matches[0] );
        },
        $content
    );
}

function vendtics_email_add_pre_header( string $content, string $pre_header ) {
    if ( empty( $pre_header ) ) {
        return $content;
    }
    return '<div style="display:none; visibility:hidden; mso-hide:all; font-size:1px; color:#fff; max-height:0; max-width:0; opacity:0; overflow:hidden;">' . $pre_header . '</div>' . $content;
}

/**
 * Deep merge two arrays.
 *
 * @param array $array1 The first array.
 * @param array $array2 The second array.
 * @return array The merged array.
 */
function vendtics_array_merge_deep( $array1, $array2 ) {
    foreach ( $array2 as $key => $value ) {
        if ( is_array( $value ) && isset( $array1[ $key ] ) && is_array( $array1[ $key ] ) ) {
            $array1[ $key ] = vendtics_array_merge_deep( $array1[ $key ], $value );
        } else {
            $array1[ $key ] = $value;
        }
    }

    return $array1;
}

function vendtics_json_maybe_decode( $value ) {
    // Check if the input is a string; JSON must be a string
    if ( is_string( $value ) ) {
        // Try to decode the JSON string
        $decoded = json_decode( $value, true );
        // Check if json_decode succeeded and the result is not null unless the value is explicitly "null"
        if ( json_last_error() === JSON_ERROR_NONE ) {
            return $decoded;
        }
    }
    // Return the original value if it's not JSON or not a string
    return $value;
}

function vendtics_bind_dynamic_values( string $content, $contact, $campaign = null ) {
    $dynamic_variable = new DynamicVariable;
    return $dynamic_variable->bind_values( $content, $contact, $campaign );
}

require_once __DIR__ . '/jwt.php';