<?php

declare(strict_types=1);

namespace Drupal\api_monitor_client;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
 * Provides a listing of Api monitor provider entities.
 */
final class ApiMonitorProviderListBuilder extends ConfigEntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader(): array {
    $header['label'] = $this->t('Label');
    $header['id'] = $this->t('Machine name');
    $header['provider_url'] = $this->t('Provider URL');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity): array {
    /** @var \Drupal\api_monitor_client\Entity\ApiMonitorProviderInterface $entity */
    $row['label'] = $entity->label();
    $row['id'] = $entity->id();
    $row['provider_url'] = $entity->getProviderUrl();
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function getOperations(EntityInterface $entity): array {
    $operations = parent::getOperations($entity);

    /** @var \Drupal\api_monitor_client\Entity\ApiMonitorProviderInterface $entity */
    $operations['add_manual_endpoint'] = [
      'title' => $this->t('Add Manual Endpoint'),
      'weight' => 5,
      'url' => Url::fromRoute('entity.api_monitor_manual.add_form', [], [
        'query' => ['provider' => $entity->id()],
      ]),
    ];

    return $operations;
  }

}
