<?php

declare(strict_types=1);

namespace Drupal\flowdrop_job;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\flowdrop\ListBuilder\FlowDropListBuilderTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a list controller for the flowdrop job entity type.
 *
 * Features sortable table headers with default sorting by changed date
 * (newest first). Displays node type reference and execution status.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class FlowDropJobListBuilder extends EntityListBuilder {

  use FlowDropListBuilderTrait;

  /**
   * The node type entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected EntityStorageInterface $nodeTypeStorage;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(
    ContainerInterface $container,
    EntityTypeInterface $entity_type,
  ): static {
    $instance = new static(
      $entity_type,
      $container->get("entity_type.manager")->getStorage($entity_type->id()),
    );
    $instance->nodeTypeStorage = $container->get("entity_type.manager")
      ->getStorage("flowdrop_node_type");
    $instance->requestStack = $container->get("request_stack");
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  protected function getSortableFields(): array {
    return ["id", "label", "status", "node_type_id", "priority", "created", "changed"];
  }

  /**
   * {@inheritdoc}
   *
   * Consistent column order: Label, Status, Node Type, Priority, Author,
   * Created, Updated.
   *
   * @return array<string, array<string, mixed>>
   *   An array of sortable header definitions.
   */
  public function buildHeader(): array {
    $header = [
      "label" => [
        "data" => $this->t("Label"),
        "field" => "label",
        "specifier" => "label",
      ],
      "status" => [
        "data" => $this->t("Status"),
        "field" => "status",
        "specifier" => "status",
      ],
      "node_type" => [
        "data" => $this->t("Node Type"),
        "field" => "node_type_id",
        "specifier" => "node_type_id",
      ],
      "priority" => [
        "data" => $this->t("Priority"),
        "field" => "priority",
        "specifier" => "priority",
      ],
      "uid" => $this->t("Author"),
      "created" => [
        "data" => $this->t("Created"),
        "field" => "created",
        "specifier" => "created",
      ],
      "changed" => [
        "data" => $this->t("Updated"),
        "field" => "changed",
        "specifier" => "changed",
        "sort" => "desc",
      ],
    ];
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   *
   * @return array<string, mixed>
   *   An array of row data for the entity.
   */
  public function buildRow(EntityInterface $entity): array {
    /** @var \Drupal\flowdrop_job\FlowDropJobInterface $entity */
    $row["label"] = $entity->toLink();
    $row["status"] = $this->buildExecutionStatusPill($entity->getStatus());
    $row["node_type"] = $this->getNodeTypeLabel($entity->get("node_type_id")->target_id ?? "");
    $row["priority"] = $entity->getPriority();
    $usernameOptions = [
      "label" => "hidden",
    ];
    $row["uid"]["data"] = $entity->get("uid")->view($usernameOptions);
    $row["created"]["data"] = $entity->get("created")->view(["label" => "hidden"]);
    $row["changed"]["data"] = $entity->get("changed")->view(["label" => "hidden"]);
    return $row + parent::buildRow($entity);
  }

  /**
   * Gets the node type label with link.
   *
   * @param string $nodeTypeId
   *   The node type ID.
   *
   * @return array<string, mixed>
   *   Render array for node type label.
   */
  protected function getNodeTypeLabel(string $nodeTypeId): array {
    return $this->buildEntityReferenceLabel(
      $nodeTypeId,
      $this->nodeTypeStorage,
      "entity.flowdrop_node_type.edit_form",
      "flowdrop_node_type",
    );
  }

}
