<?php

declare(strict_types=1);

namespace Drupal\flowdrop_session;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\flowdrop_session\Entity\FlowDropSessionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a list controller for session entities.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class FlowDropSessionListBuilder extends EntityListBuilder {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected DateFormatterInterface $dateFormatter;

  /**
   * Constructs a new FlowDropSessionListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(
    EntityTypeInterface $entity_type,
    EntityStorageInterface $storage,
    DateFormatterInterface $date_formatter,
  ) {
    parent::__construct($entity_type, $storage);
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): static {
    $entity_type_manager = $container->get('entity_type.manager');
    $storage = $entity_type_manager->getStorage($entity_type->id());
    return new static(
      $entity_type,
      $storage,
      $container->get('date.formatter'),
    );
  }

  /**
   * {@inheritdoc}
   *
   * @return array<string, array{data: \Drupal\Core\StringTranslation\TranslatableMarkup, specifier?: string}>
   *   An array of header columns.
   */
  public function buildHeader(): array {
    $header['name'] = [
      'data' => $this->t('Name'),
      'specifier' => 'name',
    ];
    $header['workflow'] = [
      'data' => $this->t('Workflow'),
    ];
    $header['status'] = [
      'data' => $this->t('Status'),
      'specifier' => 'status',
    ];
    $header['owner'] = [
      'data' => $this->t('Owner'),
    ];
    $header['created'] = [
      'data' => $this->t('Created'),
      'specifier' => 'created',
    ];
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   *
   * @return array<string, array{data?: mixed}|mixed>
   *   A renderable array representing the row.
   */
  public function buildRow(EntityInterface $entity): array {
    if (!$entity instanceof FlowDropSessionInterface) {
      return parent::buildRow($entity);
    }

    $workflow = $entity->getWorkflow();
    $owner = $entity->getOwner();

    $row['name'] = $entity->getName();
    $row['workflow'] = $workflow !== NULL ? $workflow->label() : $this->t('Unknown');
    $row['status'] = $entity->getStatus();
    $row['owner'] = $owner !== NULL ? $owner->getDisplayName() : $this->t('Anonymous');
    $row['created'] = $this->dateFormatter->format($entity->get('created')->value, 'short');

    return $row + parent::buildRow($entity);
  }

}
