<?php

namespace BitApps\SocialPro\HTTP\Controllers;

use BitApps\SocialPro\Config;
use BitApps\SocialPro\Deps\BitApps\WPKit\Http\Request\Request;
use BitApps\SocialPro\Deps\BitApps\WPKit\Http\Response;
use BitApps\SocialPro\Deps\BitApps\WPTelemetry\Telemetry\Telemetry;

final class PluginImprovementController
{
    public function getData()
    {
        return Response::success([
            'allowTracking' => Config::getOption('allow_tracking'),
        ]);
    }

    public function createOrUpdate(Request $request)
    {
        $validatedData = (object) $request->validate([
            'allowTracking' => ['required', 'boolean'],
        ]);

        if ($validatedData->allowTracking) {
            Telemetry::report()->trackingOptIn();
        } else {
            Telemetry::report()->trackingOptOut();
        }

        return Response::success([
            'allowTracking' => $validatedData->allowTracking,
        ]);
    }
}
