<?php

namespace ReviewX\Rest\Controllers;

\defined("ABSPATH") || exit;
use ReviewX\Services\SettingService;
use ReviewX\Utilities\Auth\Client;
use ReviewX\Utilities\Helper;
use Throwable;
use ReviewX\WPDrill\Response;
class SettingController
{
    protected SettingService $settingService;
    public function __construct()
    {
        $this->settingService = new SettingService();
    }
    public function getApiReviewSettingsOnSync($cpt_type = 'product') : array
    {
        $data = ["cpt_type" => $cpt_type];
        $response = $this->settingService->getApiReviewSettings($data);
        return \json_decode($response, \true);
    }
    public function getApiReviewSettings($request) : Response
    {
        $cpt_type = \strtolower($request->get_param('cpt_type')) ? \strtolower($request->get_param('cpt_type')) : \false;
        $data = ["cpt_type" => $cpt_type];
        $response = $this->settingService->getApiReviewSettings($data);
        return Helper::getApiResponse($response);
    }
    public function wooCommerceVerificationRating()
    {
        $response = $this->settingService->wooCommerceVerificationRating();
        return $response;
    }
    public function wooVerificationRatingRequired()
    {
        $response = $this->settingService->wooVerificationRatingRequired();
        return $response;
    }
    public function userSettingsAccess($request)
    {
        $data = $request->get_param('user_access');
        if (\is_array($data)) {
            \update_option('__user_setting_access', \json_encode($data));
        }
    }
    public function saveApiReviewSettings($request)
    {
        try {
            $request_params = $request->get_params();
            $response = $this->settingService->saveApiReviewSettings($request_params);
            if ($response->getStatusCode() === Response::HTTP_OK) {
                // Update Review Settings
                $review_settings = $response->getApiData()['review_settings'];
                $post_type = $request['post_type'] ?? 'product';
                if (!isset($request_params['multicriteria']) || !\is_array($request_params['multicriteria']) || $request_params['multicriteria'] === []) {
                    $review_settings = $this->settingService->preserveStoredMulticriteria($review_settings, $post_type, \true);
                }
                $this->settingService->syncWooCommerceOptionsFromReviewSettings($review_settings, $post_type);
                $this->settingService->updateReviewSettings($review_settings, $post_type);
            }
            return Helper::saasResponse($response);
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('Review settings saved failed', $e->getCode());
        }
    }
    public function wooCommerceVerificationRatingUpdate($request)
    {
        try {
            $response = $this->settingService->wooCommerceVerificationRatingUpdate($request->get_params());
            return $response;
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('Review settings saved failed', $e->getCode());
        }
    }
    public function wooVerificationRating($request)
    {
        try {
            $response = $this->settingService->wooVerificationRating($request->get_params());
            return $response;
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('Review settings saved failed', $e->getCode());
        }
    }
    public function getApiWidgetSettingsOnSync() : array
    {
        $response = $this->settingService->getApiWidgetSettings();
        return \json_decode($response, \true);
    }
    public function getApiWidgetSettings() : Response
    {
        $response = $this->settingService->getApiWidgetSettings();
        return Helper::getApiResponse($response);
    }
    public function userCurrentPlan()
    {
        $response = $this->settingService->userCurrentPlan();
        return Helper::getApiResponse($response);
    }
    public function saveApiWidgetSettings($request)
    {
        try {
            $response = $this->settingService->saveWidgetSettings($request->get_params());
            if ($response->getStatusCode() === Response::HTTP_OK) {
                // Update Widget Settings
                $widget_settings = $response->getApiData()['widget_settings'];
                $this->settingService->updateWidgetSettings($widget_settings);
            }
            return Helper::saasResponse($response);
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('Widget settings saved failed', $e->getCode());
        }
    }
    public function getApiGeneralSettings()
    {
        $response = $this->settingService->getApiGeneralSettings();
        return Helper::getApiResponse($response);
    }
    public function saveApiGeneralSettings($request)
    {
        try {
            $response = $this->settingService->saveApiGeneralSettings($request->get_params());
            return Helper::saasResponse($response);
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('General settings saved failed', $e->getCode());
        }
    }
    public function getSettingsData()
    {
        $data = ["review_settings" => ["reviews" => ["review_submission_policy" => ["options" => ["anyone" => \true, "verified_customer" => \false]], "show_verified_badge" => \false, "review_eligibility" => ["pending_payment" => \false, "processing" => \false, "on_hold" => \false, "completed_payment" => \true, "cancelled" => \false, "refunded" => \false, "failed" => \false, "draft" => \false], "auto_approve_reviews" => \true, "show_reviewer_name" => \true, "show_reviewer_country" => \true, "enable_likes_dislikes" => ["enabled" => \true, "options" => ["allow_likes" => \true, "allow_dislikes" => \false]], "allow_review_sharing" => \true, "allow_review_titles" => \true, "photo_reviews_allowed" => \true, "video_reviews_allowed" => \true, "allow_recommendations" => \true, "anonymous_reviews_allowed" => \true, "show_consent_checkbox" => \true, "allow_multiple_reviews" => \true, "multi_criteria_reviews" => ["enabled" => \true, "criteria" => ["Quality", "Price", "Size"]]]]];
        $json_data = \json_encode($data);
        \update_option('your_option_name', $json_data);
    }
    public function dataSyncStatus()
    {
        return ['sync' => Client::getSync() ? \true : \false];
    }
    public function allSettingsSave($request)
    {
        try {
            $response = $this->settingService->allSettingsSave($request->get_params());
            return Helper::saasResponse($response);
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('General settings saved failed', $e->getCode());
        }
    }
    public function removeCredentials($request)
    {
        try {
            $response = $this->settingService->removeCredentials($request->get_headers());
            return $response;
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('Sites Table Data deletion failed', $e->getCode());
        }
    }
    public function updateSiteData($request)
    {
        try {
            $response = $this->settingService->updateSiteData($request->get_headers());
            return $response;
        } catch (Throwable $e) {
            return Helper::rvxApi(['error' => $e->getMessage()])->fails('Sites Table Data update failed', $e->getCode());
        }
    }
}
