<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

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

/**
 * Delete Meta CAPI Mapping Endpoint
 */
class DeleteMetaCapiMappingEndpoint extends AbstractEndpoint
{
  public function getRoute(): string
  {
    return '/meta-capi/mappings/(?P<id>\d+)';
  }

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

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

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

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

    $result = ConversionAutomationService::deleteMetaMapping($id);

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

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