<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

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

/**
 * Get Meta CAPI Connections Endpoint
 */
class GetMetaCapiConnectionsEndpoint extends AbstractEndpoint
{
  public function getRoute(): string
  {
    return '/meta-capi/connections';
  }

  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)
  {
    $connections = MetaCapiConnectionService::getAllConnections(false);

    return $this->success([
      'connections' => array_map(fn($c) => $c->toPublicArray(), $connections),
    ]);
  }
}
