<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Services\AuxFramework\ElementClassesService;

class QaPairEntity
{
  protected ?string $question;
  protected ?string $answer;
  protected ?array $link = null;
  protected bool $centered = false;
  protected bool $showLabels = false;

  public function __construct(?string $question, ?string $answer) {
    $this->question = $question;
    $this->answer = $answer;
  }

  public function setLink(array|string|null $link): self
  {
    if (empty($link)) {
      $link = null;
    }

    $this->link = $link;

    return $this;
  }

  public function setCentered(bool $centered): self
  {
    $this->centered = $centered;
    return $this;
  }

  public function setShowLabels(bool $showLabels): self
  {
    $this->showLabels = $showLabels;
    return $this;
  }

  public function __toString(): string
  {
    $cardClasses = ElementClassesService::buildClasses('qa-card');
    if ($this->centered) {
      $cardClasses->addModifier('centered');
    }

    $questionLabel = $answerLabel = '';
    if ($this->showLabels) {
      $questionLabel = '<span class="question__label">question</span>';
      $answerLabel = '<span class="answer__label">answer</span>';
    }

    $link = '';
    if ($this->link) {
      $link = sprintf('<a href="%s" class="link--cta">%s</a>', $this->link['url'], $this->link['title']);
    }

    $string = <<<HTML
      <div class="$cardClasses">
        <div class="qa-card__question">
          $questionLabel
          <h2>{$this->question}</h2>
        </div>
        <div class="qa-card__answer">
          $answerLabel
          <p>{$this->answer}</p>
          $link
        </div>
      </div>
    HTML;

    return $string;
  }

  public static function create(?string $question, ?string $answer): static
  {
    $qaPair = new static($question, $answer);

    return $qaPair;
  }
}
