<?php

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

class Niwa_AI_Humanizer {
    const SOURCE_SKILL_SLUG    = 'ai-humanizer';
    const SOURCE_SKILL_VERSION = '2.1.0';
    const AUTOFIX_PROFILE      = 'openclaw-humanizer-autoFix-v2.1.0';

    /**
     * OpenClaw ai-humanizer v2.1.0 safe autofix replacements.
     *
     * @var array<int, array{0: string, 1: string}>
     */
    private static $safe_fill_replacements = array(
        array( '/\bin order to\b/iu', 'to' ),
        array( '/\bdue to the fact that\b/iu', 'because' ),
        array( '/\bat this point in time\b/iu', 'now' ),
        array( '/\bin the event that\b/iu', 'if' ),
        array( '/\bhas the ability to\b/iu', 'can' ),
        array( '/\bfor the purpose of\b/iu', 'to' ),
        array( '/\bfirst and foremost\b/iu', 'first' ),
        array( '/\bin light of the fact that\b/iu', 'because' ),
        array( '/\bin the realm of\b/iu', 'in' ),
        array( '/\butilize\b/iu', 'use' ),
        array( '/\butilizing\b/iu', 'using' ),
        array( '/\butilization\b/iu', 'use' ),
    );

    /**
     * OpenClaw ai-humanizer v2.1.0 chatbot opening artifacts.
     *
     * @var array<int, string>
     */
    private static $chatbot_opening_patterns = array(
        '/^(Here is|Here\'s) (a |an |the )?(comprehensive |brief |quick )?(overview|summary|breakdown|list|guide|explanation|look)[^.]*\.\s*/iu',
        '/^(Of course|Certainly|Absolutely|Sure)!\s*/iu',
        '/^(Great|Excellent|Good|Wonderful|Fantastic) question!\s*/iu',
        '/^(That\'s|That is) a (great|excellent|good|wonderful|fantastic) (question|point)!\s*/iu',
    );

    /**
     * OpenClaw ai-humanizer v2.1.0 chatbot closing artifacts.
     *
     * @var array<int, string>
     */
    private static $chatbot_closing_patterns = array(
        '/\s*(I hope this helps|Let me know if you(\'d| would) like|Feel free to|Don\'t hesitate to|Is there anything else)[^.]*[.!]\s*$/iu',
        '/\s*Happy to help[.!]?\s*$/iu',
    );

    /**
     * Apply the OpenClaw ai-humanizer v2.1.0 safe autofix profile.
     *
     * NIWA keeps one stricter always-on transport rule on top: visible em/en
     * dashes are normalized to plain punctuation so customer and founder
     * replies never ship with those marks.
     *
     * @param string $text Input text.
     * @return string Humanized text.
     */
    public function apply( $text ) {
        return $this->auto_fix( $text );
    }

    /**
     * Apply deterministic safe fixes from the upstream skill.
     *
     * @param string $text Input text.
     * @return string Fixed text.
     */
    public function auto_fix( $text ) {
        $out = (string) $text;

        if ( '' === trim( $out ) ) {
            return $out;
        }

        $out = str_replace(
            array(
                "\xE2\x80\x9C",
                "\xE2\x80\x9D",
                "\xE2\x80\x98",
                "\xE2\x80\x99",
            ),
            array( '"', '"', "'", "'" ),
            $out
        );

        foreach ( self::$safe_fill_replacements as $entry ) {
            $out = preg_replace( $entry[0], $entry[1], $out );
        }

        foreach ( self::$chatbot_opening_patterns as $pattern ) {
            $out = preg_replace( $pattern, '', $out );
        }

        foreach ( self::$chatbot_closing_patterns as $pattern ) {
            $out = preg_replace( $pattern, '', $out );
        }

        $out = str_replace( "\xE2\x80\xA6", '...', $out );
        $out = preg_replace( '/(?<=\d)\s*[\x{2013}\x{2014}]\s*(?=\d)/u', '-', $out );
        $out = preg_replace( '/(?<=[\p{L}\p{M}])\s*[\x{2013}\x{2014}]\s*(?=[\p{L}\p{M}])/u', ', ', $out );
        $out = preg_replace( '/\s+[\x{2013}\x{2014}]\s+/u', ', ', $out );
        $out = preg_replace( '/[\x{2013}\x{2014}]/u', '-', $out );

        $out = preg_replace( '/[^\S\n]{2,}/', ' ', $out );
        $out = preg_replace( '/^\s+$/m', '', $out );
        $out = preg_replace( '/\n{3,}/', "\n\n", $out );

        return trim( (string) $out );
    }

    /**
     * Return the upstream skill manifest used to keep this class in sync.
     *
     * @return array{slug: string, version: string, autofix_profile: string}
     */
    public function get_source_skill_manifest() {
        return array(
            'slug'            => self::SOURCE_SKILL_SLUG,
            'version'         => self::SOURCE_SKILL_VERSION,
            'autofix_profile' => self::AUTOFIX_PROFILE,
        );
    }
}
