<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;

class TestimonialEntity
{
  protected ?string $quote;
  protected ?string $name = null;
  protected ?string $title = null;
  protected ?string $organization = null;
  protected ?array $image = null;

  public function __construct(?string $quote) {
    $this->quote = $quote;
  }

  public function setContact(
    string|array|null $name = null,
    ?string $title = null,
    ?string $organization = null
  ): static
  {
    if (is_array($name)) {
      $this->setContact($name['name'], $name['title'], $name['organization'] ?? $name['org']);
    } else {
      $this->name = $name;
      $this->title = $title;
      $this->organization = $organization;
    }

    return $this;
  }

  public function setImage(array|string|null|bool $image): static
  {
    if (empty($image)) {
      $image = null;
    }

    $this->image = $image;

    return $this;
  }

  public function __toString(): string
  {
    if (!$this->quote) {
      return '';
    }

    $pullquote = QuoteEntity::createPullquote($this->quote)
      ->setCiteName($this->name)
      ->setCiteTitle($this->title)
      ->setCiteOrganization($this->organization);

    $image = '';
    if ($this->image) {
      $image = ImageEntity::create($this->image)->wrap('testimonial-image');
    }

    $string = <<<HTML
      <div class="testimonial-card">
        $image
        $pullquote
      </div>
    HTML;

    return $string;
  }

  public static function create(?string $quote): static
  {
    $testimonial = new static($quote);

    return $testimonial;
  }
}
