<?php

namespace Drupal\nodehive_core_space_dashboard\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    $route = $collection->get('entity.nodehive_space.canonical');
    if ($route) {
      $route->addDefaults(['_controller' => '\Drupal\nodehive_core_space_dashboard\Controller\SpaceRedirectController::toContent']);
    }

    // Remove the default dashboard_id from the content planner route so it
    // only matches /space/{id}/dashboard/{dashboard_id} (explicit ID).
    // This allows our static route to handle /space/{id}/dashboard.
    $cp_route = $collection->get('content_planner.nodehive_space_dashboard.dashboard');
    if ($cp_route) {
      $defaults = $cp_route->getDefaults();
      unset($defaults['dashboard_id']);
      $cp_route->setDefaults($defaults);
    }
  }

}
