<?php

namespace Hostinger\Surveys;

use Hostinger\WpHelper\Config;
use Hostinger\WpHelper\Utils as Helper;

defined('ABSPATH') || exit;

class SurveyManager
{
    public const CACHE_THREE_HOURS = 3600 * 3;
    public const TIME_24_HOURS = 86400;
    public const CLIENT_SURVEY_IDENTIFIER = 'customer_satisfaction_score';
    public const CLIENT_WOO_COMPLETED_ACTIONS = 'woocommerce_task_list_tracked_completed_tasks';
    public const SUBMITTED_SURVEY_TRANSIENT = 'submitted_survey_transient';
    public const IS_CLIENT_ELIGIBLE_TRANSIENT_RESPONSE = 'client_eligibility_transient_response';
    public const IS_CLIENT_ELIGIBLE_TRANSIENT_REQUEST = 'survey_questions_transient_request';
    public const LOCATION_SLUG = 'location';
    public const HIDE_SURVEY_TRANSIENT = 'hts_hide_survey';

    private Config $configHandler;
    private Helper $helper;
    private Rest $surveysRest;

    public function __construct(
        Helper $helper,
        Config $configHandler,
        Rest $surveysRest
    ) {
        $this->helper        = $helper;
        $this->configHandler = $configHandler;
        $this->surveysRest   = $surveysRest;
    }

    /**
     * Get WooCommerce admin pages paths
     *
     * @return array Array of WooCommerce admin page paths
     */
    public function getWooCommercePages(): array
    {
        $admin_path = parse_url(admin_url(), PHP_URL_PATH);

        $pages = [
            'admin.php?page=wc-admin',
            'edit.php?post_type=shop_order',
            'admin.php?page=wc-admin&path=/customers',
            'edit.php?post_type=shop_coupon&legacy_coupon_menu=1',
            'admin.php?page=wc-admin&path=/marketing',
            'admin.php?page=wc-reports',
            'admin.php?page=wc-orders',
            'admin.php?page=wc-settings',
            'admin.php?page=wc-status',
            'admin.php?page=wc-admin&path=/extensions',
            'edit.php?post_type=product',
            'post-new.php?post_type=product',
            'edit.php?post_type=product&page=product-reviews',
            'edit.php?post_type=product&page=product_attributes',
            'edit-tags.php?taxonomy=product_cat&post_type=product',
            'edit-tags.php?taxonomy=product_tag&post_type=product',
            'admin.php?page=wc-admin&path=/analytics/overview',
            'admin.php?page=wc-admin',
        ];

        $woocommerce_pages = [];
        foreach ($pages as $page) {
            $woocommerce_pages[] = $admin_path . $page;
        }

        return $woocommerce_pages;
    }

    /**
     * Check if current page is a WooCommerce admin page
     *
     * @return bool True if current page is a WooCommerce admin page
     */
    public function isWoocommerceAdminPage(): bool
    {
        if (! isset($_SERVER['REQUEST_URI'])) {
            return false;
        }

        $currentUri = sanitize_text_field($_SERVER['REQUEST_URI']);

        if (defined('DOING_AJAX') && \DOING_AJAX) {
            return false;
        }

        if (isset($currentUri) && strpos($currentUri, '/wp-json/') !== false) {
            return false;
        }

        $woocommercePages = $this->getWooCommercePages();

        foreach ($woocommercePages as $page) {
            if (strpos($currentUri, $page) !== false) {
                return true;
            }
        }

        return false;
    }

    public function isClientEligible(): bool
    {
        $transientRequestKey  = self::IS_CLIENT_ELIGIBLE_TRANSIENT_REQUEST;
        $transientResponseKey = self::IS_CLIENT_ELIGIBLE_TRANSIENT_RESPONSE;
        $cachedEligibility    = get_transient($transientResponseKey);

        // Check if a request is already in progress
        if (get_transient('hts_eligible_request')) {
            return false;
        }

        // Check if transient response exists
        if ($cachedEligibility && ( $cachedEligibility === 'eligible' || $cachedEligibility === 'not_eligible' )) {
            return $cachedEligibility === 'eligible';
        }

        // Attempt to set transient request
        if (! $this->helper->checkTransientEligibility($transientRequestKey, self::CACHE_THREE_HOURS)) {
            return false;
        }

        try {
            // Set transient flag to indicate request in progress
            set_transient('hts_eligible_request', 'in_progress', 60);

            $isEligible = $this->surveysRest->isClientEligible();

            // Clear the request transient flag
            delete_transient('hts_eligible_request');

            if ($isEligible) {
                set_transient($transientResponseKey, 'eligible', self::CACHE_THREE_HOURS);

                return $isEligible;
            }

            set_transient($transientResponseKey, 'not_eligible', self::CACHE_THREE_HOURS);

            return false;
        } catch (\Exception $exception) {
            $this->helper->errorLog('Error checking eligibility: ' . $exception->getMessage());

            return false;
        }
    }

    public function submitSurveyAnswers(array $answers, string $surveyId, string $surveyLocation, int $is_mobile = 0): void
    {
        $data = [
            'identifier' => self::CLIENT_SURVEY_IDENTIFIER,
            'is_mobile'  => $is_mobile,
            'answers'    => [
                [
                    'question_slug' => self::LOCATION_SLUG,
                    'answer'        => $surveyLocation,
                ]
            ],
        ];

        $answers = $this->addUserAnswers($data, $answers);

        $success = $this->surveysRest->submitSurveyData($answers);

        set_transient(self::SUBMITTED_SURVEY_TRANSIENT, true, self::TIME_24_HOURS);

        if ($success) {
            delete_transient(self::IS_CLIENT_ELIGIBLE_TRANSIENT_RESPONSE);
	        $settingKey = $surveyId . '_survey_completed';
	        update_option('hostinger_' . $settingKey, true);
        }

        wp_send_json('Survey completed');

        if (has_action('litespeed_purge_all')) {
            do_action('litespeed_purge_all');
        }
    }

    public function getSpecifiedSurvey(array $activeSurvey): array
    {
        $specifiedSurveyQuestions = [
            'pages'               => [],
            'showQuestionNumbers' => 'off',
            'showTOC'             => false,
            'pageNextText'        => __('Next', 'hostinger-wp-surveys'),
            'pagePrevText'        => __('Previous', 'hostinger-wp-surveys'),
            'completeText'        => __('Submit', 'hostinger-wp-surveys'),
            'completedHtml'       => __('Thank you for completing the survey !', 'hostinger-wp-surveys'),
            'requiredText'        => '*',
        ];

        foreach ($activeSurvey['questions'] as $question) {
            $element = [
                'type'              => $question['type'],
                'name'              => $question['slug'],
                'title'             => $question['question'],
                'requiredErrorText' => __('Response required.', 'hostinger-wp-surveys'),
            ];

            if ($question['slug'] == 'comment') {
                $element['maxLength'] = 250;
            }

            if ($question['slug'] == 'score') {
                $element['rateMin']            = '1';
                $element['rateMax']            = '10';
                $element['minRateDescription'] = __('Poor', 'hostinger-wp-surveys');
                $element['maxRateDescription'] = __('Excellent', 'hostinger-wp-surveys');
            }

            if ($question['isRequired']) {
                $element['isRequired'] = true;
            }

            $questionData = [
                'name'     => $question['slug'],
                'elements' => [ $element ],
            ];

            $specifiedSurveyQuestions['pages'][] = $questionData;
        }

        return $specifiedSurveyQuestions;
    }

    public static function getHostingerSurveys(): array
    {
        return apply_filters('hostinger_add_surveys', []);
    }

    public function generateSurveyHtml(): string
    {
        $allSurveys = SurveyManager::getHostingerSurveys();

        if (empty($allSurveys)) {
            return '';
        }

        $activeSurvey = $this->getHighestPrioritySurvey($allSurveys);
        ob_start();
        ?>
        <div class="hts-survey-wrapper hts-survey"
             data-survey-id="<?php echo esc_attr($activeSurvey['id']) ?>"
             data-location="<?php echo esc_attr($activeSurvey['location']) ?>"
             data-surveys="<?php echo esc_attr(json_encode($this->getSpecifiedSurvey($activeSurvey))) ?>">
            <div class="close-btn">
                <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="100" height="100" viewBox="0 0 24 24">
                    <path d="M 4.9902344 3.9902344 A 1.0001 1.0001 0 0 0 4.2929688 5.7070312 L 10.585938 12 L 4.2929688 18.292969 A 1.0001 1.0001 0 1 0 5.7070312 19.707031 L 12 13.414062 L 18.292969 19.707031 A 1.0001 1.0001 0 1 0 19.707031 18.292969 L 13.414062 12 L 19.707031 5.7070312 A 1.0001 1.0001 0 0 0 18.980469 3.9902344 A 1.0001 1.0001 0 0 0 18.292969 4.2929688 L 12 10.585938 L 5.7070312 4.2929688 A 1.0001 1.0001 0 0 0 4.9902344 3.9902344 z"></path>
                </svg>
            </div>
            <div id="hostinger-feedback-survey" class="hostinger-feedback-survey"></div>
            <div id="hts-questionsLeft">
                <span id="hts-currentQuestion">1</span>
                <?php
                echo esc_html(
                        __(
                                'Question',
                                'hostinger-wp-surveys'
                        )
                );
                ?>
                <?php echo esc_html(__('of ', 'hostinger-wp-surveys')); ?>
                <span id="hts-allQuestions"></span>
            </div>
        </div>
        <?php if ( isset($activeSurvey['review_url']) ) { ?>
            <div id="hts-survey-review"
                 class="hts-survey-wrapper"
                 data-review-min-required-score="<?php echo esc_attr($activeSurvey['review_min_required_score']) ?>">
                <div class="close-btn">
                    <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="100" height="100" viewBox="0 0 24 24">
                        <path d="M 4.9902344 3.9902344 A 1.0001 1.0001 0 0 0 4.2929688 5.7070312 L 10.585938 12 L 4.2929688 18.292969 A 1.0001 1.0001 0 1 0 5.7070312 19.707031 L 12 13.414062 L 18.292969 19.707031 A 1.0001 1.0001 0 1 0 19.707031 18.292969 L 13.414062 12 L 19.707031 5.7070312 A 1.0001 1.0001 0 0 0 18.980469 3.9902344 A 1.0001 1.0001 0 0 0 18.292969 4.2929688 L 12 10.585938 L 5.7070312 4.2929688 A 1.0001 1.0001 0 0 0 4.9902344 3.9902344 z"></path>
                    </svg>
                </div>
                <div class="hostinger-feedback-survey">
                    <div class="sd-question__header">
                        <h5><?php echo esc_html(__( 'Rate this plugin on WordPress.org', 'hostinger-wp-surveys' ) ); ?></h5>
                    </div>
                    <div class="hts-survey-stars">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                  clip-rule="evenodd"
                                  d="M12 17.27L18.18 21L16.54 13.97L22 9.24L14.81 8.63L12 2L9.19 8.63L2 9.24L7.46 13.97L5.82 21L12 17.27Z"
                                  fill="#673DE6"/>
                        </svg>
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                  clip-rule="evenodd"
                                  d="M12 17.27L18.18 21L16.54 13.97L22 9.24L14.81 8.63L12 2L9.19 8.63L2 9.24L7.46 13.97L5.82 21L12 17.27Z"
                                  fill="#673DE6"/>
                        </svg>
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                  clip-rule="evenodd"
                                  d="M12 17.27L18.18 21L16.54 13.97L22 9.24L14.81 8.63L12 2L9.19 8.63L2 9.24L7.46 13.97L5.82 21L12 17.27Z"
                                  fill="#673DE6"/>
                        </svg>
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                  clip-rule="evenodd"
                                  d="M12 17.27L18.18 21L16.54 13.97L22 9.24L14.81 8.63L12 2L9.19 8.63L2 9.24L7.46 13.97L5.82 21L12 17.27Z"
                                  fill="#673DE6"/>
                        </svg>
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                  clip-rule="evenodd"
                                  d="M12 17.27L18.18 21L16.54 13.97L22 9.24L14.81 8.63L12 2L9.19 8.63L2 9.24L7.46 13.97L5.82 21L12 17.27Z"
                                  fill="#673DE6"/>
                        </svg>
                    </div>
                    <div class="hts-survey-footer">
                        <a href="<?php echo esc_url($activeSurvey['review_url']); ?>" target="_blank" class="hts-submit-review sd-btn"><?php echo esc_html(__( 'Add review', 'hostinger-wp-surveys' ) ); ?></a>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php
        return ob_get_clean();
    }


    public function defaultWoocommerceSurveyCompleted(): bool
    {
        $completedActions         = get_option(self::CLIENT_WOO_COMPLETED_ACTIONS, []);
        $requiredCompletedActions = [ 'products', 'payments' ];

        return empty(array_diff($requiredCompletedActions, $completedActions));
    }

    public function getOldestProductDate(): int
    {
        global $wpdb;

        $getProductDate = $wpdb->prepare(
            "
	        SELECT MIN(post_date) 
	        FROM {$wpdb->posts} 
	        WHERE post_type = %s
	        AND post_status = %s
        ",
            'product',
            'publish'
        );

        $oldestProductDate = $wpdb->get_var($getProductDate);

        if ($oldestProductDate !== null) {
            $oldestProductDateTimestamp = strtotime($oldestProductDate);

            if ($oldestProductDateTimestamp !== false) {
                return $oldestProductDateTimestamp;
            }
        }

        return strtotime('-1 year');
    }

    public function isTimeElapsed(string $firstLoginAt, int $timeInSeconds): bool
    {
        $currentTime = time();
        $timeElapsed = $currentTime - $timeInSeconds;

        return $timeElapsed >= $firstLoginAt;
    }

    public function activeSurveyHtml(): void
    {
        if (! did_action('activeSurvey')) {
            $survey_html = $this->generateSurveyHtml('');
            echo $survey_html;
            do_action('activeSurvey');
        }
    }

    /**
     * @param string $surveyId The unique ID for this survey
     * @param string $scoreQuestion The question for the score step
     * @param string $commentQuestion The question for the comment step
     * @param string $location The location of the survey
     * @param int $priority The priority of the survey. The bigger the number, the higher the priority
     * @param string|null $review_url Optional. The url to review the plugin
     * @param int $review_min_required_score The minimum rating in the score step to show the Review step
     * @return array
     */
    public static function addSurvey(string $surveyId, string $scoreQuestion, string $commentQuestion, string $location, int $priority, string $review_url = null, int $review_min_required_score = 7 ): array
    {
        $surveyData = [
            'id'        => $surveyId,
            'questions' => [
                [
                    'type'       => 'rating',
                    'slug'       => 'score',
                    'question'   => $scoreQuestion,
                    'isRequired' => true
                ],
                [
                    'type'       => 'comment',
                    'slug'       => 'comment',
                    'question'   => $commentQuestion,
                    'isRequired' => false
                ],
            ],
            'location'  => $location,
            'priority'  => $priority,
            'review_url' => $review_url,
            'review_min_required_score' => $review_min_required_score,
        ];

        $surveys[] = $surveyData;

        return $surveys;
    }

    public function getHighestPrioritySurvey(array $allSurveys): array
    {
        $surveys = array_merge([], ...$allSurveys);

        return array_reduce($surveys, function ($highestPrioritySurvey, $currentSurvey) {
            return ( ! isset($highestPrioritySurvey['priority']) ||
                     ( isset($currentSurvey['priority']) && $currentSurvey['priority'] > $highestPrioritySurvey['priority'] ) )
                ? $currentSurvey : $highestPrioritySurvey;
        }, []);
    }

    /**
     * Checks if a specific survey is not completed
     * @param string $surveyId
     * @return bool
     */
    public function isSurveyNotCompleted(string $surveyId): bool
    {
        $optionKey = 'hostinger_' . $surveyId . '_survey_completed';
        $notCompleted = ! get_option($optionKey, '');
        return $notCompleted;
    }

    private function addUserAnswers(array $data, array $answers): array
    {
        foreach ($answers as $answerSlug => $answer) {
            $data['answers'][] = [
                'question_slug' => $answerSlug,
                'answer'        => $answer,
            ];
        }

        return $data;
    }

    public function isSurveyHidden(): bool
    {
        $surveyVisibility = get_transient(self::HIDE_SURVEY_TRANSIENT);
        return $surveyVisibility !== false;
    }
}
