<?php

declare(strict_types=1);

namespace Drupal\flowdrop_pipeline;

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 pipeline entity type.
 *
 * Features sortable table headers with default sorting by changed date
 * (newest first). Displays workflow reference and execution status.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class FlowDropPipelineListBuilder extends EntityListBuilder {

  use FlowDropListBuilderTrait;

  /**
   * The workflow entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected EntityStorageInterface $workflowStorage;

  /**
   * {@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->workflowStorage = $container->get("entity_type.manager")
      ->getStorage("flowdrop_workflow");
    $instance->requestStack = $container->get("request_stack");
    return $instance;
  }

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

  /**
   * {@inheritdoc}
   *
   * Consistent column order: Label, Status, Workflow, 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",
      ],
      "workflow" => [
        "data" => $this->t("Workflow"),
        "field" => "workflow_id",
        "specifier" => "workflow_id",
      ],
      "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_pipeline\Entity\FlowDropPipelineInterface $entity */
    $row["label"] = $entity->toLink();
    $row["status"] = $this->buildExecutionStatusPill($entity->getStatus());
    $row["workflow"] = $this->getWorkflowLabel($entity->getWorkflowId());
    $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 workflow label with link.
   *
   * @param string $workflowId
   *   The workflow ID.
   *
   * @return array<string, mixed>
   *   Render array for workflow label.
   */
  protected function getWorkflowLabel(string $workflowId): array {
    return $this->buildEntityReferenceLabel(
      $workflowId,
      $this->workflowStorage,
      "flowdrop.workflow.edit",
      "flowdrop_workflow",
    );
  }

}
