<?php
declare(strict_types=1);

namespace AuxCF\Entities\Field;

use AuxCF\Entities\Field\Enum\FieldTypeEnum;
use AuxCF\Entities\Field\Enum\PostFieldReturnFormatEnum;
use AuxCF\Entities\Field\Enum\RelationshipFieldReturnFormatEnum;
use AuxCF\Entities\Field\Enum\TaxonomyFieldReturnFormatEnum;
use AuxCF\Entities\Field\Enum\TaxonomyFieldTypeEnum;
use AuxCF\Entities\Field\Traits\AllowNullTraits;
use AuxCF\Entities\FieldEntity;
use AuxCF\Entities\Settings\Enum\IntSwitchEnum;
use AuxCF\Entities\Settings\Traits\EnumPropertyTraits;

/**
 * ACF "Relationship Field" for selecting one or more posts
 * @link https://www.advancedcustomfields.com/resources/relationship/
 * @since 1.0.0
 */
class RelationshipFieldEntity extends FieldEntity
{
  use EnumPropertyTraits;

  protected FieldTypeEnum $type = FieldTypeEnum::Relationship;

  protected array $postType = [];
  protected array $postStatus = [];
  protected RelationshipFieldReturnFormatEnum $returnFormat = RelationshipFieldReturnFormatEnum::Id;
  protected ?int $min = null;
  protected ?int $max = null;
  protected array $filters = ['search', 'post_type', 'taxonomy'];
  protected array $elements = ['featured_image'];

  /**
   * @since 1.0.0
   */
  public function getPostType(): array
  {
    return $this->postType;
  }

  /**
   * @since 1.0.0
   */
  public function setPostType(array $postType): self
  {
    $this->postType = $postType;
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function addPostType(array|string $postType): self
  {
    $this->postType = array_unique(array_merge($this->postType, (array) $postType));
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function getPostStatus(): array
  {
    return $this->postStatus;
  }

  /**
   * @since 1.0.0
   */
  public function setPostStatus(array $postStatus): self
  {
    $this->postStatus = $postStatus;
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function setFilters(array $filters): self
  {
    $this->filters = $filters;
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function setElements(array $elements): self
  {
    $this->elements = $elements;
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function setMin(?int $min): self
  {
    $this->min = $min;
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function setMax(?int $max): self
  {
    $this->max = $max;
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function setReturnFormat(string|PostFieldReturnFormatEnum $returnFormat): self
  {
    $this->setEnumProperty(PostFieldReturnFormatEnum::class, $returnFormat, 'return_format');
    return $this;
  }

  /**
   * @since 1.0.0
   */
  public function setMultiple(int|IntSwitchEnum $multiple): self
  {
    $this->setEnumProperty(IntSwitchEnum::class, $multiple, 'multiple');
    return $this;
  }

  /**
   * Prepare the array of field parameters specific to the "Relationship Field"
   * @since 1.0.0
   */
  public function getFieldArray(): array
  {
    /**
     * If the "postType" is defined and contains only one available post type,
     * remove option to filter by "post_type" since all posts will only be that
     * post type
     */
    $filters = $this->filters;
    if (!empty($this->postType) && count($this->postType) === 1) {
      if (($index = array_search('post_type', $filters)) !== false) {
        unset($filters[$index]);
      }
    }

    $array = [
      'return_format' => $this->returnFormat->value,
      'taxonomy' => '',
      'post_type' => $this->postType,
      'post_status' => $this->postStatus,
      'min' => $this->min,
      'max' => $this->max,
      'elements' => $this->elements,
      'filters' => $filters,
      'bidirectional' => 0,
      'bidirectional_target' => [],
    ];

    return $array;
  }
}
