<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

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

/**
 * Get Meta CAPI Mappings Endpoint
 */
class GetMetaCapiMappingsEndpoint extends AbstractEndpoint
{
  public function getRoute(): string
  {
    return '/meta-capi/connections/(?P<connection_id>\d+)/mappings';
  }

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

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

  public function getRouteParams(): array
  {
    return [
      'connection_id' => [
        'type' => 'integer',
        'required' => true,
      ]
    ];
  }

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

    $mappings = ConversionAutomationService::getMetaMappings($connectionId);

    return [
      'success' => true,
      'mappings' => $mappings
    ];
  }
}
