<?php

namespace PloverCRM\Modules\Integrations\Endpoints;

use PloverCRM\Core\Api\AbstractEndpoint;
use PloverCRM\Modules\Integrations\Services\DataSourceService;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

/**
 * Get Categories Endpoint
 * 
 * Returns all data source categories with their providers
 */
class GetCategoriesEndpoint extends AbstractEndpoint
{
  public function getRoute(): string
  {
    return '/integrations/categories';
  }

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

  public function handle(WP_REST_Request $request): WP_REST_Response|WP_Error
  {
    $service = new DataSourceService($this->container);
    $activeOnly = $request->get_param('active_only') === 'true';

    $categories = $activeOnly
      ? $service->getActiveCategories()
      : $service->getCategories();

    return new WP_REST_Response([
      'success' => true,
      'data' => $categories,
    ], 200);
  }
}
