<?php
declare(strict_types=1);

/**
 * EnglishSpeak
 * Pedagogical Pronunciation Feedback Engine
 */

final class PronunciationFeedbackEngine
{
    private array $focuses;
    private array $messages;
    private array $rules;

    public function __construct(string $engineDirectory)
    {
        $this->focuses = require $engineDirectory . '/pronunciation_focus.php';
        $this->messages = require $engineDirectory . '/pronunciation_messages.php';
        $this->rules = require $engineDirectory . '/pronunciation_rules.php';
    }

    public function getFocus(string $focusKey): array
    {
        $fallback = (string)($this->rules['fallback_focus'] ?? 'none');

        return $this->focuses[$focusKey]
            ?? $this->focuses[$fallback]
            ?? [
                'label' => 'None',
                'type' => 'none',
                'sounds' => [],
            ];
    }

    public function getRules(): array
    {
        return $this->rules;
    }

    public function getMessages(): array
    {
        return $this->messages;
    }

    public function buildStudentConfig(string $focusKey = 'none'): array
    {
        $focus = $this->getFocus($focusKey);

        return [
            'focus_key' => $focusKey,
            'focus' => [
                'label' => (string)($focus['label'] ?? 'None'),
                'type' => (string)($focus['type'] ?? 'none'),
                'sounds' => array_values(
                    array_map(
                        'strval',
                        is_array($focus['sounds'] ?? null)
                            ? $focus['sounds']
                            : []
                    )
                ),
            ],
            'score_thresholds' =>
                $this->rules['score_thresholds'] ?? [],
            'pedagogical_rules' =>
                $this->rules['pedagogical_rules'] ?? [],
            'messages' => $this->messages,
        ];
    }
}