// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { AI_CORE_PREFERENCES_TITLE, PREFERENCE_NAME_SERVER_SIDE_COMPACTION } from '@theia/ai-core/lib/common/ai-core-preferences';
import { SERVER_SIDE_COMPACTION_TOKEN_THRESHOLD_MINIMUM } from '@theia/ai-core/lib/common/language-model';
import { LINUX_ENV_HINT, nls, PreferenceSchema } from '@theia/core';

export const API_KEY_PREF = 'ai-features.openAiOfficial.openAiApiKey';
export const MODELS_PREF = 'ai-features.openAiOfficial.officialOpenAiModels';
export const USE_RESPONSE_API_PREF = 'ai-features.openAiOfficial.useResponseApi';
export const SERVER_SIDE_COMPACTION_PREF = 'ai-features.openAiOfficial.serverSideCompaction';
export const SERVER_SIDE_COMPACTION_TOKEN_THRESHOLD_PREF = 'ai-features.openAiOfficial.serverSideCompactionTokenThreshold';
export const CUSTOM_ENDPOINTS_PREF = 'ai-features.openAiCustom.customOpenAiModels';

export const OpenAiPreferencesSchema: PreferenceSchema = {
    properties: {
        [API_KEY_PREF]: {
            type: 'string',
            markdownDescription: nls.localize('theia/ai/openai/apiKey/mdDescription',
                'Enter an API Key of your official OpenAI Account. **Please note:** By using this preference the Open AI API key will be stored in clear text \
on the machine running Theia. Use the environment variable `OPENAI_API_KEY` to set the key securely.') + LINUX_ENV_HINT,
            title: AI_CORE_PREFERENCES_TITLE,
        },
        [MODELS_PREF]: {
            type: 'array',
            description: nls.localize('theia/ai/openai/models/description', 'Official OpenAI models to use'),
            title: AI_CORE_PREFERENCES_TITLE,
            default: [
                'gpt-5.6-sol',
                'gpt-5.6-terra',
                'gpt-5.6-luna',
                'gpt-5.5',
                'gpt-5.5-pro'
            ],
            items: {
                type: 'string'
            }
        },
        [USE_RESPONSE_API_PREF]: {
            type: 'boolean',
            default: false,
            title: AI_CORE_PREFERENCES_TITLE,
            markdownDescription: nls.localize('theia/ai/openai/useResponseApi/mdDescription',
                'Use the newer OpenAI Response API instead of the Chat Completion API for official OpenAI models. ' +
                'This setting only applies to official OpenAI models - custom providers must configure this individually.')
        },
        [SERVER_SIDE_COMPACTION_PREF]: {
            type: 'string',
            enum: ['default', 'enabled', 'disabled'],
            enumDescriptions: [
                nls.localize('theia/ai/openai/compaction/default', 'Follow the global chat server-side compaction setting.'),
                nls.localize('theia/ai/openai/compaction/enabled', 'Always request server-side compaction for official OpenAI models.'),
                nls.localize('theia/ai/openai/compaction/disabled', 'Never request server-side compaction for official OpenAI models.')
            ],
            default: 'default',
            markdownDescription: nls.localize('theia/ai/openai/compaction/description',
                'Override provider-native server-side compaction for official OpenAI models. This applies to the OpenAI Response API only; ' +
                'the Chat Completions API ignores it. "default" follows the global chat setting ({0}). When effectively ' +
                'enabled, the Response API is asked to summarize older turns once the conversation grows past the provider\'s threshold.',
                `\`#${PREFERENCE_NAME_SERVER_SIDE_COMPACTION}#\``),
            title: AI_CORE_PREFERENCES_TITLE,
        },
        [SERVER_SIDE_COMPACTION_TOKEN_THRESHOLD_PREF]: {
            type: 'integer',
            minimum: SERVER_SIDE_COMPACTION_TOKEN_THRESHOLD_MINIMUM,
            markdownDescription: nls.localize('theia/ai/openai/compactionTokenThreshold/description',
                'Override the global input-token threshold for server-side compaction for official OpenAI models. When unset, the global setting or provider default applies. ' +
                'If set, the value must be at least 50,000 tokens.'),
            title: AI_CORE_PREFERENCES_TITLE,
        },
        [CUSTOM_ENDPOINTS_PREF]: {
            type: 'array',
            title: AI_CORE_PREFERENCES_TITLE,
            markdownDescription: nls.localize('theia/ai/openai/customEndpoints/mdDescription',
                'Integrate custom models compatible with the OpenAI API, for example via `vllm`. The required attributes are `model` and `url`.\
            \n\
            Optionally, you can\
            \n\
            - specify a unique `id` to identify the custom model in the UI. If none is given `model` will be used as `id`.\
            \n\
            - provide an `apiKey` to access the API served at the given url. Use `true` to indicate the use of the global OpenAI API key.\
            \n\
            - provide an `apiVersion` to access the API served at the given url in Azure. Use `true` to indicate the use of the global OpenAI API version.\
            \n\
            - provide a `deployment` name for your Azure deployment.\
            \n\
            - set `developerMessageSettings` to one of `user`, `system`, `developer`, `mergeWithFollowingUserMessage`, or `skip` to control how the developer message is\
            included (where `user`, `system`, and `developer` will be used as a role, `mergeWithFollowingUserMessage` will prefix the following user message with the system\
            message or convert the system message to user message if the next message is not a user message. `skip` will just remove the system message).\
            Defaulting to `developer`.\
            \n\
            - specify `supportsStructuredOutput: false` to indicate that structured output shall not be used.\
            \n\
            - specify `enableStreaming: false` to indicate that streaming shall not be used.\
            \n\
            - specify `useResponseApi: true` to use the newer OpenAI Response API instead of the Chat Completion API (requires compatible endpoint).\
            \n\
            - specify `reasoningSupport` to opt in to the chat reasoning selector. Provide an object with\
            `supportedLevels` (e.g. `["off", "low", "medium", "high", "auto"]`) and an optional `defaultLevel`.\
            \n\
            Refer to [our documentation](https://theia-ide.org/docs/user_ai/#openai-compatible-models-eg-via-vllm) for more information.'),
            default: [],
            items: {
                type: 'object',
                properties: {
                    model: {
                        type: 'string',
                        title: nls.localize('theia/ai/openai/customEndpoints/modelId/title', 'Model ID')
                    },
                    url: {
                        type: 'string',
                        title: nls.localize('theia/ai/openai/customEndpoints/url/title', 'The Open AI API compatible endpoint where the model is hosted')
                    },
                    id: {
                        type: 'string',
                        title: nls.localize('theia/ai/openai/customEndpoints/id/title', 'A unique identifier which is used in the UI to identify the custom model'),
                    },
                    apiKey: {
                        type: ['string', 'boolean'],
                        title: nls.localize('theia/ai/openai/customEndpoints/apiKey/title',
                            'Either the key to access the API served at the given url or `true` to use the global OpenAI API key'),
                    },
                    apiVersion: {
                        type: ['string', 'boolean'],
                        title: nls.localize('theia/ai/openai/customEndpoints/apiVersion/title',
                            'Either the version to access the API served at the given url in Azure or `true` to use the global OpenAI API version'),
                    },
                    deployment: {
                        type: 'string',
                        title: nls.localize('theia/ai/openai/customEndpoints/deployment/title',
                            'The deployment name to access the API served at the given url in Azure'),
                    },
                    developerMessageSettings: {
                        type: 'string',
                        enum: ['user', 'system', 'developer', 'mergeWithFollowingUserMessage', 'skip'],
                        default: 'developer',
                        title: nls.localize('theia/ai/openai/customEndpoints/developerMessageSettings/title',
                            'Controls the handling of system messages: `user`, `system`, and `developer` will be used as a role, `mergeWithFollowingUserMessage` will prefix\
                         the following user message with the system message or convert the system message to user message if the next message is not a user message.\
                         `skip` will just remove the system message), defaulting to `developer`.')
                    },
                    supportsStructuredOutput: {
                        type: 'boolean',
                        title: nls.localize('theia/ai/openai/customEndpoints/supportsStructuredOutput/title',
                            'Indicates whether the model supports structured output. `true` by default.'),
                    },
                    enableStreaming: {
                        type: 'boolean',
                        title: nls.localize('theia/ai/openai/customEndpoints/enableStreaming/title',
                            'Indicates whether the streaming API shall be used. `true` by default.'),
                    },
                    useResponseApi: {
                        type: 'boolean',
                        title: nls.localize('theia/ai/openai/customEndpoints/useResponseApi/title',
                            'Use the newer OpenAI Response API instead of the Chat Completion API. `false` by default for custom providers.'
                            + 'Note: Will automatically fall back to Chat Completions API when tools are used.'),
                    },
                    reasoningSupport: {
                        type: 'object',
                        title: nls.localize('theia/ai/openai/customEndpoints/reasoningSupport/title',
                            'Declares the model\'s reasoning capabilities. When set the chat shows a reasoning selector for this model.'),
                        properties: {
                            supportedLevels: {
                                type: 'array',
                                items: {
                                    type: 'string',
                                    enum: ['off', 'minimal', 'low', 'medium', 'high', 'auto']
                                }
                            },
                            defaultLevel: {
                                type: 'string',
                                enum: ['off', 'minimal', 'low', 'medium', 'high', 'auto']
                            }
                        }
                    }
                }
            }
        }
    }
};
