<?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 endpoint configuration from a site root URL.
   *
   * Appends the FlowDrop API path to the provided site URL and delegates to
   * generateEndpointConfig(). Use this in controllers that obtain the site
   * root via Url::fromRoute('<front>') with absolute = TRUE.
   *
   * @param string $site_url
   *   The absolute site root URL (e.g. 'https://example.com/'). A trailing
   *   slash is stripped before the API path is appended.
   *
   * @return array<string, mixed>
   *   See generateEndpointConfig().
   */
  public function generateAbsoluteEndpointConfig(string $site_url): array {
    return $this->generateEndpointConfig(rtrim($site_url, '/') . '/api/flowdrop');
  }

  /**
   * Generate the default endpoint configuration for FlowDrop.
   *
   * @param string $base_url
   *   The base URL for the API endpoints
   *   (e.g. 'https://example.com/api/flowdrop').
   *
   * @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',
        ],
        'pipelines' => [
          'list' => '/workflow/{workflow_id}/pipelines',
          'get' => '/pipeline/{id}',
          'create' => '/pipeline',
          'update' => '/pipeline/{id}',
          'delete' => '/pipeline/{id}',
          'status' => '/pipeline/{id}/status',
          'logs' => '/pipeline/{id}/logs',
          'execute' => '/pipeline/{id}/execute',
          'stop' => '/pipeline/{id}/stop',
        ],
        'templates' => [
          'list' => '/templates',
          'get' => '/templates/{id}',
          'create' => '/templates',
          'update' => '/templates/{id}',
          'delete' => '/templates/{id}',
        ],
        'users' => [
          'profile' => '/users/profile',
          'preferences' => '/users/preferences',
        ],
        'chat' => [
          'sendMessage' => '/workflows/{id}/chat/messages',
          'getHistory' => '/workflows/{id}/chat/messages',
          'clearHistory' => '/workflows/{id}/chat/messages',
        ],
        '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'),
        ],
      ],
    ];
  }

}
