<?php
namespace EasyChat\Library\AiProvider;

require_once __DIR__ . '/openai.php';







class EasyTokensProvider extends OpenAI {
    const OFFICIAL_SPACE = 'official_easychat_docs';

    public function __construct($token, $data = []) {
        $data['host'] = 'https://api.easytokens.ru';
        if (empty($data['model'])) {
            $data['model'] = 'easylite';
        }
        parent::__construct($token, $data);
    }

    public function supportsKnowledgeGrounding() {
        return true;
    }

    public function supportsGroundingSources() {
        return true;
    }

    protected function extendChatPayload(array $data, array $options) {
        if (!array_key_exists('knowledge', $options)) {
            return $data;
        }
        $knowledge = $this->normalizeKnowledgePayload($options['knowledge']);
        if ($knowledge === null) {
            return $data;
        }
        unset($data['web']);
        $data['knowledge'] = $knowledge;
        $data['think'] = 'off';
        $data['temperature'] = min(0.15, isset($data['temperature']) ? (float)$data['temperature'] : 0.15);
        return $data;
    }

    private function normalizeKnowledgePayload($value) {
        if (!is_array($value)) {
            throw new \InvalidArgumentException('knowledge must be a server-built object.');
        }
        $space = strtolower(trim((string)($value['space'] ?? '')));
        if ($space !== self::OFFICIAL_SPACE) {
            throw new ProviderException('Недоступное пространство знаний.', 'knowledge_space_forbidden', 403);
        }
        $profile = strtolower(trim((string)($value['profile'] ?? '')));
        if (!in_array($profile, ['easychat_product_support', 'easychat_admin_help'], true)) {
            throw new ProviderException('Недоступный профиль справки.', 'knowledge_space_forbidden', 403);
        }
        $locale = strtolower(trim((string)($value['locale'] ?? 'ru')));
        $locale = $locale === 'en' ? 'en' : 'ru';
        $mode = strtolower(trim((string)($value['mode'] ?? 'required')));
        if (!in_array($mode, ['required', 'auto', 'never'], true)) {
            $mode = 'required';
        }
        $payload = [
            'space' => self::OFFICIAL_SPACE,
            'profile' => $profile,
            'locale' => $locale,
            'mode' => $mode,
            'max_sources' => max(1, min(4, (int)($value['max_sources'] ?? 4))),
            'return_sources' => !isset($value['return_sources']) || (bool)$value['return_sources'],
        ];
        $version = trim((string)($value['product_version'] ?? ''));
        if ($version !== '' && preg_match('/^[0-9]+(?:\.[0-9A-Za-z-]+){0,3}$/', $version) === 1) {
            $payload['product_version'] = substr($version, 0, 32);
        }
        $platform = strtolower(trim((string)($value['platform'] ?? '')));
        if ($platform !== '' && preg_match('/^[a-z0-9_-]{1,40}$/', $platform) === 1) {
            $payload['platform'] = $platform;
        }
        $route = trim((string)($value['route'] ?? ''));
        if ($route !== '' && preg_match('/^[a-zA-Z0-9_\/-]{1,160}$/', $route) === 1) {
            $payload['route'] = $route;
        }
        return $payload;
    }
}
