<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

use WP_REST_Request;
use PloverCRM\Core\Api\AbstractEndpoint;
use PloverCRM\Modules\Integrations\Services\ConversionAutomationService;

class DeleteGoogleMappingEndpoint extends AbstractEndpoint
{
  public function getRoute(): string
  {
    return '/google/mappings/(?P<id>\d+)';
  }

  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)
  {
    $id = absint($request->get_param('id'));
    $deleted = ConversionAutomationService::deleteGoogleMapping($id);

    if (!$deleted) {
      return new \WP_Error('delete_failed', 'Failed to delete Google Ads mapping', ['status' => 404]);
    }

    return [
      'success' => true
    ];
  }
}
