<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

use WP_REST_Request;
use PloverCRM\Core\Api\AbstractEndpoint;
use PloverCRM\Modules\Integrations\Services\AutomationsLogger;
use PloverCRM\Modules\Integrations\Services\GoogleAdsConnectionService;

class DisconnectGoogleAuthEndpoint extends AbstractEndpoint
{
  public function getRoute(): string
  {
    return '/google/auth';
  }

  public function getMethod()
  {
    return 'DELETE';
  }

  public function checkPermission(WP_REST_Request $request)
  {
    return $this->checkAuthenticatedCapability($request, 'manage_options');
  }

  public function handle(WP_REST_Request $request)
  {
    $deletedConnections = GoogleAdsConnectionService::deleteAllConnections();
    delete_option('plover_crm_google_config');
    (new AutomationsLogger())->info('Google authorization disconnected', [
      '_activity' => [
        'module' => 'integrations',
        'feature' => 'connections',
      ],
      'deleted_connections' => $deletedConnections,
    ]);
    return $this->success([
      'disconnected' => true,
      'deletedConnections' => $deletedConnections,
    ]);
  }
}
