<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Arcai_Chunker {

	const WORDS_PER_CHUNK = 700;
	const OVERLAP_WORDS   = 80;

	/**
	 * Split a post into plain-text chunks ready for embedding.
	 *
	 * @param WP_Post $post
	 * @return array[] Each: [ 'content' => string, 'token_estimate' => int ]
	 */
	public static function chunk_post( $post ) {
		$text = self::plain_text( $post );
		if ( '' === $text ) {
			return array();
		}

		$header = $post->post_title . "\n";
		$words  = preg_split( '/\s+/', $text, -1, PREG_SPLIT_NO_EMPTY );
		$chunks = array();
		$step   = self::WORDS_PER_CHUNK - self::OVERLAP_WORDS;

		for ( $start = 0; $start < count( $words ); $start += $step ) {
			$slice   = array_slice( $words, $start, self::WORDS_PER_CHUNK );
			$content = $header . implode( ' ', $slice );
			$chunks[] = array(
				'content'        => $content,
				'token_estimate' => self::estimate_tokens( $content ),
			);
			if ( $start + self::WORDS_PER_CHUNK >= count( $words ) ) {
				break;
			}
		}

		return $chunks;
	}

	public static function plain_text( $post ) {
		$text = $post->post_title . "\n" . $post->post_excerpt . "\n" . $post->post_content;
		$text = strip_shortcodes( $text );
		$text = excerpt_remove_blocks( $text );
		$text = wp_strip_all_tags( $text );
		$text = html_entity_decode( $text, ENT_QUOTES, 'UTF-8' );
		return trim( preg_replace( '/\s+/', ' ', $text ) );
	}

	/**
	 * Rough token estimate (~4 chars per token for English text).
	 */
	public static function estimate_tokens( $text ) {
		return (int) ceil( mb_strlen( $text ) / 4 );
	}
}
