<?php

declare(strict_types=1);

namespace App\Intent;

use App\Config\SalesIntentConfig;

final class SalesIntentLite
{
    public const DISCOVERY = 'discovery';
    public const PRICING = 'pricing';
    public const COMPARISON = 'comparison';
    public const OBJECTION = 'objection';
    public const IMPLEMENTATION = 'implementation';
    public const ROI = 'roi';

    public function __construct(
        private readonly SalesIntentConfig $config
    ) {
    }

    public function detect(string $originalPrompt): array
    {
        $p = $this->normalize($originalPrompt);

        if ($p === '') {
            return [
                'intent' => self::DISCOVERY,
                'score' => 0,
            ];
        }

        $scores = [
            self::PRICING => 0,
            self::COMPARISON => 0,
            self::OBJECTION => 0,
            self::IMPLEMENTATION => 0,
            self::ROI => 0,
        ];

        // ------------------------------------------------------------
        // PRICING
        // ------------------------------------------------------------
        foreach ($this->config->getSalesSignals() as $word) {
            if (preg_match('/\b' . preg_quote($word, '/') . '\b/u', $p)) {
                $scores[self::PRICING] += 3;
            }
        }

        // ------------------------------------------------------------
        // COMPARISON
        // ------------------------------------------------------------
        foreach ($this->config->getComparisonSignals() as $pattern) {
            if (preg_match($pattern, $p)) {
                $scores[self::COMPARISON] += 3;
            }
        }

        // ------------------------------------------------------------
        // OBJECTION
        // ------------------------------------------------------------
        foreach ($this->config->getObjectionSignals() as $word) {
            if (preg_match('/\b' . preg_quote($word, '/') . '\b/u', $p)) {
                $scores[self::OBJECTION] += 3;
            }
        }

        // ------------------------------------------------------------
        // IMPLEMENTATION
        // ------------------------------------------------------------
        foreach ($this->config->getImplementationSignals() as $word) {
            if (preg_match('/\b' . preg_quote($word, '/') . '\b/u', $p)) {
                $scores[self::IMPLEMENTATION] += 3;
            }
        }

        // ------------------------------------------------------------
        // ROI
        // ------------------------------------------------------------
        foreach ($this->config->getRoiSignals() as $word) {
            if (preg_match('/\b' . preg_quote($word, '/') . '\b/u', $p)) {
                $scores[self::ROI] += 3;
            }
        }

        // ------------------------------------------------------------
        // Entscheidung
        // ------------------------------------------------------------

        arsort($scores);

        $intents = array_keys($scores);
        $values = array_values($scores);

        $topIntent = $intents[0] ?? self::DISCOVERY;
        $topScore = $values[0] ?? 0;
        $secondScore = $values[1] ?? 0;

        // Kein relevanter Score → Discovery
        if ($topScore < $this->config->getMinScoreThreshold()) {
            return [
                'intent' => self::DISCOVERY,
                'score' => 0,
            ];
        }

        // Keine klare Dominanz → Discovery
        if (($topScore - $secondScore) < $this->config->getDominanceDelta()) {
            return [
                'intent' => self::DISCOVERY,
                'score' => $topScore,
            ];
        }

        return [
            'intent' => $topIntent,
            'score' => $topScore,
        ];
    }

    private function normalize(string $s): string
    {
        $s = mb_strtolower($s);

        return strtr($s, [
            'ä' => 'ae',
            'ö' => 'oe',
            'ü' => 'ue',
            'ß' => 'ss',
        ]);
    }
}