<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

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

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

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

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

  public function handle(WP_REST_Request $request)
  {
    $connectionId = (int) $request->get_param('id');

    try {
      $mappings = ConversionAutomationService::getGoogleMappings($connectionId);

      return [
        'success' => true,
        'mappings' => $mappings
      ];
    } catch (\Exception $e) {
      return new \WP_Error('db_error', $e->getMessage(), ['status' => 500]);
    }
  }
}
