<?php
/**
 * News Handler Class
 *
 * Handles news/blog post display in the chat widget.
 *
 * @package Askany
 * @since 1.0.0
 */

namespace Askany;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * News Handler class.
 */
class NewsHandler {

	/**
	 * Get news settings.
	 *
	 * @return array News settings.
	 */
	public function get_news_settings() {
		return array(
			'enabled' => get_option( 'askany_news_enabled', false ),
			'label' => get_option( 'askany_news_label', __( 'News', 'askany' ) ),
			'mode' => get_option( 'askany_news_mode', 'latest' ),
			'timeframe' => get_option( 'askany_news_timeframe', 7 ),
			'count' => get_option( 'askany_news_count', 5 ),
			'excerpt_length' => get_option( 'askany_news_excerpt_length', 150 ),
			'selected_posts' => get_option( 'askany_news_selected_posts', array() ),
		);
	}

	/**
	 * Get news posts based on settings.
	 *
	 * @return array Array of formatted posts.
	 */
	public function get_news_posts() {
		$settings = $this->get_news_settings();

		if ( ! $settings['enabled'] ) {
			return array();
		}

		if ( $settings['mode'] === 'latest' ) {
			return $this->get_latest_posts( $settings );
		} else {
			return $this->get_selected_posts( $settings );
		}
	}

	/**
	 * Get latest posts based on timeframe.
	 *
	 * @param array $settings News settings.
	 * @return array Array of formatted posts.
	 */
	private function get_latest_posts( $settings ) {
		$args = array(
			'post_type' => 'post',
			'post_status' => 'publish',
			'posts_per_page' => $settings['count'],
			'orderby' => 'date',
			'order' => 'DESC',
			'date_query' => array(
				array(
					'after' => $settings['timeframe'] . ' days ago',
				),
			),
		);

		$posts = get_posts( $args );
		return $this->format_posts( $posts, $settings );
	}

	/**
	 * Get selected posts.
	 *
	 * @param array $settings News settings.
	 * @return array Array of formatted posts.
	 */
	private function get_selected_posts( $settings ) {
		if ( empty( $settings['selected_posts'] ) ) {
			return array();
		}

		$args = array(
			'post_type' => 'post',
			'post_status' => 'publish',
			'post__in' => $settings['selected_posts'],
			'orderby' => 'post__in',
			'posts_per_page' => $settings['count'],
		);

		$posts = get_posts( $args );
		return $this->format_posts( $posts, $settings );
	}

	/**
	 * Format posts for display.
	 *
	 * @param array $posts Array of WP_Post objects.
	 * @param array $settings News settings.
	 * @return array Array of formatted posts.
	 */
	private function format_posts( $posts, $settings ) {
		$formatted = array();

		foreach ( $posts as $post ) {
			$excerpt = wp_strip_all_tags( $post->post_excerpt );
			if ( empty( $excerpt ) ) {
				$excerpt = wp_strip_all_tags( $post->post_content );
			}
			
			// Trim to specified length
			if ( strlen( $excerpt ) > $settings['excerpt_length'] ) {
				$excerpt = substr( $excerpt, 0, $settings['excerpt_length'] ) . '...';
			}

			$formatted[] = array(
				'id' => $post->ID,
				'title' => get_the_title( $post ),
				'excerpt' => $excerpt,
				'image' => get_the_post_thumbnail_url( $post, 'medium' ),
				'date' => get_the_date( '', $post ),
				'author' => get_the_author_meta( 'display_name', $post->post_author ),
				'url' => get_permalink( $post ),
			);
		}

		return $formatted;
	}

	/**
	 * Get full post content.
	 *
	 * @param int $post_id Post ID.
	 * @return array|null Post data or null if not found.
	 */
	public function get_full_post( $post_id ) {
		$post = get_post( $post_id );

		if ( ! $post || $post->post_status !== 'publish' ) {
			return null;
		}

		// Setup post data for proper content filtering
		setup_postdata( $post );

		$content = apply_filters( 'the_content', $post->post_content );
		
		// Reset post data
		wp_reset_postdata();

		return array(
			'id' => $post->ID,
			'title' => get_the_title( $post ),
			'content' => $content,
			'image' => get_the_post_thumbnail_url( $post, 'large' ),
			'date' => get_the_date( '', $post ),
			'author' => get_the_author_meta( 'display_name', $post->post_author ),
			'url' => get_permalink( $post ),
		);
	}

	/**
	 * Get all published posts for selection.
	 *
	 * @param int $limit Number of posts to retrieve.
	 * @return array Array of posts with ID and title.
	 */
	public function get_all_posts_for_selection( $limit = 50 ) {
		$args = array(
			'post_type' => 'post',
			'post_status' => 'publish',
			'posts_per_page' => $limit,
			'orderby' => 'date',
			'order' => 'DESC',
		);

		$posts = get_posts( $args );
		$result = array();

		foreach ( $posts as $post ) {
			$result[] = array(
				'id' => $post->ID,
				'title' => get_the_title( $post ),
				'date' => get_the_date( '', $post ),
			);
		}

		return $result;
	}
}
