<?php

namespace Vendtics\App\Services;

defined( 'ABSPATH' ) || exit;

use stdClass;

class DynamicVariable {
    protected stdClass $contact;

    protected ?stdClass $campaign;

    public function bind_values( string $content, $contact, $campaign = null ) {
        if ( is_int( $contact ) ) {
            $contact = vendtics_contact_repository()->get_by_id( $contact );
        }

        $this->contact  = $contact;
        $this->campaign = $campaign;

        $content = str_replace(
            [
                "https://{{",
                "https://www.{{",
                "http://{{",
                "http://www.{{"
            ],
            "{{",
            $content
        );

        preg_match_all( '/\{{(.*?)\}}/', $content, $matches );

        $values_methods = $this->values_methods();
        $dynamic_values = [];

        foreach ( $matches[1] as $key => $variable ) {
            // Split the variable by dot notation
            $parts = explode( '.', $variable );
            $base  = array_shift( $parts );

            // Check if the value is already cached
            if ( ! isset( $dynamic_values[$variable] ) ) {
                $dynamic_value = '';

                if ( isset( $values_methods[$base] ) && is_callable( $values_methods[$base] ) ) {
                    // Resolve the object property if applicable
                    $dynamic_value = call_user_func( $values_methods[$base], $parts[0] ?? '' );
                }
                $dynamic_values[$variable] = $dynamic_value;
            }

            // Replace the placeholder with the resolved value
            $content = str_replace( $matches[0][$key], $dynamic_values[$variable], $content );
        }

        return $content;
    }

    protected function values_methods() {
        return apply_filters(
            'vendtics_dynamic_values_callbacks', [
                'site_url'                    => 'site_url',
                'site_title'                  => function() {
                    return get_bloginfo( 'name' );
                },
                // 'user'       => function( $property ) {
                //     $user = wp_get_current_user();
                //     return isset( $user->$property ) ? $user->$property : '';
                // },
                'contact'                     => function( $property ) {
                    return isset( $this->contact->$property ) ? $this->contact->$property : '';
                },
                'business'                    => function( $property ) {
                    $repository        = vendtics_settings_repository();
                    $business_settings = $repository->get_settings()['business'];
                    
                    if ( 'logo_url' === $property ) {
                        return $business_settings['company_logo']['url'] ?? '';
                    }

                    return isset( $business_settings[$property] ) ? $business_settings[$property] : '';
                },
                'un_subscription_confirm_url' => function() {
                    $data = [
                        'contact_id' => $this->contact->id
                    ];

                    if ( $this->campaign ) {
                        $data['campaign_id'] = $this->campaign->id;
                    }

                    $repository       = vendtics_settings_repository();
                    $general_settings = $repository->get_settings()['general'];
                    return add_query_arg( ['token' => vendtics_generate_jwt_token( $data )], get_permalink( intval( $general_settings['unsubscribe_confirm_page'] ) ) );
                },
                'subscription_confirm_url'    => function() {
                    $token = vendtics_generate_jwt_token( ["contact_id" => $this->contact->id] );
                    return add_query_arg( ['token' => $token], get_rest_url( null, "vendtics/double-optin/confirm" ) );
                }
            ]
        );
    }
}