<?php

declare(strict_types=1);

namespace Drupal\flowdrop_ui_components\Service;

use Drupal\Core\Access\CsrfTokenGenerator;

/**
 * Service for generating FlowDrop endpoint configuration.
 *
 * Provides a standardized way to generate API endpoint configurations
 * for the FlowDrop workflow editor and related UI components.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
class FlowDropEndpointConfigService {

  /**
   * Constructs a FlowDropEndpointConfigService.
   *
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrfTokenGenerator
   *   The CSRF token generator service.
   */
  public function __construct(
    protected readonly CsrfTokenGenerator $csrfTokenGenerator,
  ) {}

  /**
   * Generate the default endpoint configuration for FlowDrop.
   *
   * @param string $base_url
   *   The base URL for the API endpoints.
   *
   * @return array<string, mixed>
   *   The endpoint configuration array containing:
   *   - baseUrl: The base URL for API calls.
   *   - endpoints: Nested array of endpoint paths by category.
   *   - timeout: Request timeout in milliseconds.
   *   - retry: Retry configuration settings.
   *   - auth: Authentication configuration with CSRF token.
   */
  public function generateEndpointConfig(string $base_url): array {
    return [
      'baseUrl' => $base_url,
      'endpoints' => [
        'nodes' => [
          'list' => '/nodes',
          'get' => '/nodes/{id}',
          'byCategory' => '/nodes?category={category}',
          'metadata' => '/nodes/{id}/metadata',
        ],
        'workflows' => [
          'list' => '/workflows',
          'get' => '/workflows/{id}',
          'create' => '/workflows',
          'update' => '/workflows/{id}',
          'delete' => '/workflows/{id}',
          'validate' => '/workflows/validate',
          'export' => '/workflows/{id}/export',
          'import' => '/workflows/import',
        ],
        'executions' => [
          'execute' => '/workflows/{id}/execute',
          'status' => '/executions/{id}',
          'cancel' => '/executions/{id}/cancel',
          'logs' => '/executions/{id}/logs',
          'history' => '/executions',
        ],
        'templates' => [
          'list' => '/templates',
          'get' => '/templates/{id}',
          'create' => '/templates',
          'update' => '/templates/{id}',
          'delete' => '/templates/{id}',
        ],
        'users' => [
          'profile' => '/users/profile',
          'preferences' => '/users/preferences',
        ],
        'system' => [
          'health' => '/system/health',
          'config' => '/system/config',
          'version' => '/system/version',
        ],
      ],
      'timeout' => 30000,
      'retry' => [
        'enabled' => TRUE,
        'maxAttempts' => 3,
        'delay' => 1000,
        'backoff' => 'exponential',
      ],
      'auth' => [
        'type' => 'custom',
        'headers' => [
          'X-CSRF-Token' => $this->csrfTokenGenerator->get('rest'),
        ],
      ],
    ];
  }

}
