<?php

namespace ModernFields\FieldGroups\Entities;

use WP_Post;

/**
 * Represents a post entity.
 * 
 * @since 1.0.0
 */
class Post extends Entity
{

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

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

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

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

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

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

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

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

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

  /**
   * @param WP_Post $post
   */
  public function __construct(WP_Post $post)
  {
    $this->ID = (int) $post->ID;
    $this->title = $post->post_title;
    $this->post_type = $post->post_type;
    $this->status = $post->post_status;
    $this->date = $post->post_date;
    $this->content = $post->post_content;
    $this->excerpt = $post->post_excerpt;
    $this->mime_type = $post->post_mime_type;
    $this->author_id = (int) $post->post_author;

    # Get all meta for this post
    $this->meta = $post->ID > 0 ? get_post_meta($post->ID) : [];
  }

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

  public function getTitle(): string
  {
    return $this->title;
  }

  public function getPostType(): string
  {
    return $this->post_type;
  }

  public function getStatus(): string
  {
    return $this->status;
  }

  public function getDate(): string
  {
    return $this->date;
  }

  public function getContent(): string
  {
    return $this->content;
  }

  public function getExcerpt(): string
  {
    return $this->excerpt;
  }

  public function getMimeType(): string
  {
    return $this->mime_type;
  }

  public function getAuthorId(): int
  {
    return $this->author_id;
  }

  public function toArray(): array
  {
    return [
      'ID' => $this->ID,
      'title' => $this->title,
      'post_type' => $this->post_type,
      'status' => $this->status,
      'date' => $this->date,
      'content' => $this->content,
      'excerpt' => $this->excerpt,
      'mime_type' => $this->mime_type,
      'author_id' => $this->author_id,
      'meta' => $this->prepareMetaForJson(),
    ];
  }
}
