<?php
/**
 * PostHelpers
 *
 * Shared post utility methods used across theme templates.
 *
 * @package Relatedly\Utilities
 * @since   2.0.0
 */

namespace Relatedly\Utilities;

defined( 'ABSPATH' ) || exit;

/**
 * Provides shared helpers for post template data.
 *
 * @since 2.0.0
 */
class PostHelpers {

	/**
	 * Get a post thumbnail URL, or empty string if none exists.
	 *
	 * @param int    $post_id Post ID.
	 * @param string $size    Registered image size.
	 * @return string URL or empty string.
	 */
	public static function thumbnail_url( int $post_id, string $size = 'thumbnail' ): string {
		$thumb_id = get_post_thumbnail_id( $post_id );
		if ( ! $thumb_id ) {
			return '';
		}
		$src = wp_get_attachment_image_src( (int) $thumb_id, $size );
		return $src ? (string) $src[0] : '';
	}

	/**
	 * Get a formatted post date.
	 *
	 * @param int    $post_id   Post ID.
	 * @param string $date_type 'published' | 'modified'.
	 * @return string Formatted date string.
	 */
	public static function formatted_date( int $post_id, string $date_type = 'published' ): string {
		if ( 'modified' === $date_type ) {
			return (string) get_the_modified_date( '', $post_id );
		}
		return (string) get_the_date( '', $post_id );
	}

	/**
	 * Get a trimmed excerpt for a post.
	 *
	 * @param \WP_Post $post       Post object.
	 * @param int      $word_count Max words.
	 * @return string Plain-text excerpt.
	 */
	public static function excerpt( \WP_Post $post, int $word_count = 20 ): string {
		$excerpt = '' !== $post->post_excerpt ? $post->post_excerpt : $post->post_content;
		return wp_trim_words( wp_strip_all_tags( $excerpt ), $word_count );
	}
}
