<?php
/**
 * Content Chunker
 * Splits large content into optimal chunks for embedding
 *
 * @package AskAny
 */

namespace Askany;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Chunker {
	private $chunk_size;
	private $min_chunk_size;
	
	public function __construct( $chunk_size = 4000 ) {
		$this->chunk_size = $chunk_size;
		$this->min_chunk_size = 500; // Don't create chunks smaller than this
	}
	
	/**
	 * Check if content needs chunking
	 */
	public function should_chunk( $text ) {
		return strlen( $text ) > $this->chunk_size;
	}
	
	/**
	 * Split text into chunks
	 */
	public function chunk_text( $text ) {
		if ( ! $this->should_chunk( $text ) ) {
			return array( $text );
		}
		
		// Split by paragraphs first
		$paragraphs = preg_split( '/\n\s*\n/', $text );
		
		$chunks = array();
		$current_chunk = '';
		
		foreach ( $paragraphs as $paragraph ) {
			$paragraph = trim( $paragraph );
			
			if ( empty( $paragraph ) ) {
				continue;
			}
			
			// If single paragraph is too large, split it
			if ( strlen( $paragraph ) > $this->chunk_size ) {
				// Save current chunk if not empty
				if ( ! empty( $current_chunk ) ) {
					$chunks[] = trim( $current_chunk );
					$current_chunk = '';
				}
				
				// Split large paragraph
				$sub_chunks = $this->split_large_paragraph( $paragraph );
				$chunks = array_merge( $chunks, $sub_chunks );
				continue;
			}
			
			// Check if adding this paragraph exceeds chunk size
			if ( strlen( $current_chunk ) + strlen( $paragraph ) + 2 > $this->chunk_size ) {
				// Save current chunk
				if ( ! empty( $current_chunk ) ) {
					$chunks[] = trim( $current_chunk );
				}
				// Start new chunk with this paragraph
				$current_chunk = $paragraph;
			} else {
				// Add to current chunk
				$current_chunk .= ( empty( $current_chunk ) ? '' : "\n\n" ) . $paragraph;
			}
		}
		
		// Add final chunk
		if ( ! empty( $current_chunk ) ) {
			$chunks[] = trim( $current_chunk );
		}
		
		// Filter out chunks that are too small
		$chunks = array_filter( $chunks, function( $chunk ) {
			return strlen( $chunk ) >= $this->min_chunk_size;
		} );
		
		return array_values( $chunks );
	}
	
	/**
	 * Split large paragraph by sentences
	 */
	private function split_large_paragraph( $paragraph ) {
		// Split by sentences
		$sentences = preg_split( '/(?<=[.!?])\s+/', $paragraph );
		
		$chunks = array();
		$current_chunk = '';
		
		foreach ( $sentences as $sentence ) {
			$sentence = trim( $sentence );
			
			if ( empty( $sentence ) ) {
				continue;
			}
			
			// If single sentence is too large, split by words
			if ( strlen( $sentence ) > $this->chunk_size ) {
				if ( ! empty( $current_chunk ) ) {
					$chunks[] = trim( $current_chunk );
					$current_chunk = '';
				}
				
				$word_chunks = $this->split_by_words( $sentence );
				$chunks = array_merge( $chunks, $word_chunks );
				continue;
			}
			
			// Check if adding this sentence exceeds chunk size
			if ( strlen( $current_chunk ) + strlen( $sentence ) + 1 > $this->chunk_size ) {
				if ( ! empty( $current_chunk ) ) {
					$chunks[] = trim( $current_chunk );
				}
				$current_chunk = $sentence;
			} else {
				$current_chunk .= ( empty( $current_chunk ) ? '' : ' ' ) . $sentence;
			}
		}
		
		if ( ! empty( $current_chunk ) ) {
			$chunks[] = trim( $current_chunk );
		}
		
		return $chunks;
	}
	
	/**
	 * Split by words (last resort)
	 */
	private function split_by_words( $text ) {
		$words = explode( ' ', $text );
		$chunks = array();
		$current_chunk = '';
		
		foreach ( $words as $word ) {
			if ( strlen( $current_chunk ) + strlen( $word ) + 1 > $this->chunk_size ) {
				if ( ! empty( $current_chunk ) ) {
					$chunks[] = trim( $current_chunk );
				}
				$current_chunk = $word;
			} else {
				$current_chunk .= ( empty( $current_chunk ) ? '' : ' ' ) . $word;
			}
		}
		
		if ( ! empty( $current_chunk ) ) {
			$chunks[] = trim( $current_chunk );
		}
		
		return $chunks;
	}
}
