<?php

namespace ModernFields\FieldGroups\Entities;

use WP_Term;

/**
 * Represents a term entity.
 * 
 * @since 1.0.0
 */
class Term extends Entity
{

  /**
   * @var int
   */
  protected int $term_id;

  /**
   * @var string
   */
  protected string $name;

  /**
   * @var string
   */
  protected string $slug;

  /**
   * @var string
   */
  protected string $taxonomy;

  /**
   * @var string
   */
  protected string $description;

  /**
   * @var int
   */
  protected int $parent;

  /**
   * @var int
   */
  protected int $count;

  /**
   * @var array
   */
  protected array $meta;

  /**
   * @param WP_Term $term
   */
  public function __construct(WP_Term $term)
  {
    $this->term_id = (int) $term->term_id;
    $this->name = $term->name;
    $this->slug = $term->slug;
    $this->taxonomy = $term->taxonomy;
    $this->description = $term->description;
    $this->parent = (int) $term->parent;
    $this->count = (int) $term->count;

    # Get all meta for this term
    $this->meta = get_term_meta($term->term_id);
  }

  public function getId(): int
  {
    return $this->term_id;
  }

  public function getName(): string
  {
    return $this->name;
  }

  public function getSlug(): string
  {
    return $this->slug;
  }

  public function getTaxonomy(): string
  {
    return $this->taxonomy;
  }

  public function getDescription(): string
  {
    return $this->description;
  }

  public function getParent(): int
  {
    return $this->parent;
  }

  public function getCount(): int
  {
    return $this->count;
  }

  public function toArray(): array
  {
    return [
      'term_id' => $this->term_id,
      'name' => $this->name,
      'slug' => $this->slug,
      'taxonomy' => $this->taxonomy,
      'description' => $this->description,
      'parent' => $this->parent,
      'count' => $this->count,
      'meta' => $this->prepareMetaForJson(),
    ];
  }
}
