<?php

namespace ModernFields\FieldGroups\Entities;

use WP_Post;

/**
 * Represents a media entity.
 * 
 * @since 1.0.0
 */
class Media extends Entity
{

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

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

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

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

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

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

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

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

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

  /**
   * @param WP_Post $post
   */
  public function __construct(WP_Post $post)
  {
    $this->ID = (int) $post->ID;
    $this->title = $post->post_title;
    $this->date = $post->post_date;
    $this->url = wp_get_attachment_url($post->ID);
    $this->description = $post->post_content;
    $this->caption = $post->post_excerpt;
    $this->alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
    $this->image_sizes = $this->getAllImageSizes($post->ID);
    $this->mime_type = $post->post_mime_type;

    # Get all meta for this post
    $this->meta = get_post_meta($post->ID);
  }

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

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

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

  public function getUrl(): string
  {
    return $this->url;
  }

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

  public function getCaption(): string
  {
    return $this->caption;
  }

  public function getAlt(): string
  {
    return $this->alt;
  }

  public function getImageSizes(): array
  {
    return $this->image_sizes;
  }

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

  /**
   * Get all registered WordPress image sizes with their dimensions, settings, and URLs.
   * 
   * @param int $attachment_id The attachment ID to get URLs for
   * @return array Array of image sizes, each containing 'name', 'width', 'height', 'crop', and 'url'
   */
  protected function getAllImageSizes(int $attachment_id): array
  {
    # Get all registered image sizes with their properties
    $registered_sizes = wp_get_registered_image_subsizes();
    $sizes = [];

    foreach ($registered_sizes as $name => $info) {
      # Get the URL for this specific image size
      $image_src = wp_get_attachment_image_src($attachment_id, $name);
      $url = $image_src ? $image_src[0] : '';

      $sizes[] = [
        'name' => $name,
        'width' => (int) $info['width'],
        'height' => (int) $info['height'],
        'crop' => (bool) $info['crop'],
        'url' => $url,
      ];
    }

    return $sizes;
  }

  public function toArray(): array
  {
    return [
      'ID' => $this->ID,
      'title' => $this->title,
      'date' => $this->date,
      'mime_type' => $this->mime_type,
      'url' => $this->url,
      'description' => $this->description,
      'caption' => $this->caption,
      'alt' => $this->alt,
      'image_sizes' => $this->image_sizes,
      'meta' => $this->prepareMetaForJson(),
    ];
  }
}
