// *****************************************************************************
// Copyright (C) 2024-2025 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 { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import { Emitter, Event, nls } from '@theia/core';
import { LanguageModelAlias, LanguageModelAliasRegistry } from '../common/language-model-alias';
import { PreferenceScope } from '@theia/core/lib/common';
import { LANGUAGE_MODEL_ALIASES_PREFERENCE } from '../common/ai-core-preferences';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { AiConfigurationService } from '../common/ai-configuration-service';

@injectable()
export class DefaultLanguageModelAliasRegistry implements LanguageModelAliasRegistry {

    protected aliases: LanguageModelAlias[] = [
        {
            id: 'default/code',
            defaultModelIds: [
                'anthropic/claude-opus-5',
                'openai/gpt-5.6-sol',
                'google/gemini-3.1-pro-preview'
            ],
            description: nls.localize('theia/ai/core/defaultModelAliases/code/description', 'Optimized for code understanding and generation tasks.')
        },
        {
            id: 'default/universal',
            defaultModelIds: [
                'anthropic/claude-opus-5',
                'openai/gpt-5.6-sol',
                'google/gemini-3.1-pro-preview'
            ],
            description: nls.localize('theia/ai/core/defaultModelAliases/universal/description', 'Well-balanced for both code and general language use.')
        },
        {
            id: 'default/code-completion',
            defaultModelIds: [
                'anthropic/claude-sonnet-5',
                'openai/gpt-5.6-sol',
                'google/gemini-3.1-pro-preview'
            ],
            description: nls.localize('theia/ai/core/defaultModelAliases/code-completion/description', 'Best suited for code autocompletion scenarios.')
        },
        {
            id: 'default/summarize',
            defaultModelIds: [
                'anthropic/claude-opus-5',
                'openai/gpt-5.6-sol',
                'google/gemini-3.1-pro-preview'
            ],
            description: nls.localize('theia/ai/core/defaultModelAliases/summarize/description', 'Models prioritized for summarization and condensation of content.')
        },
        {
            id: 'default/fast',
            defaultModelIds: [
                'anthropic/claude-haiku-4-5',
                'openai/gpt-5.6-luna',
                'google/gemini-3.6-flash'
            ],
            description: nls.localize('theia/ai/core/defaultModelAliases/fast/description',
                'Faster and cheaper models for simpler tasks like exploration or basic tool calling, where deep reasoning is not required.')
        }
    ];
    protected readonly onDidChangeEmitter = new Emitter<void>();
    readonly onDidChange: Event<void> = this.onDidChangeEmitter.event;

    @inject(AiConfigurationService)
    protected readonly aiConfigurationService: AiConfigurationService;

    protected readonly _ready = new Deferred<void>();
    get ready(): Promise<void> {
        return this._ready.promise;
    }

    @postConstruct()
    protected init(): void {
        this.aiConfigurationService.ready.then(() => {
            this.loadFromPreference();
            this.aiConfigurationService.onDidChange(change => {
                if (change.affectsPreference(LANGUAGE_MODEL_ALIASES_PREFERENCE)) {
                    this.loadFromPreference();
                    if (!change.preferenceName) {
                        this.onDidChangeEmitter.fire();
                    }
                }
            });
            this._ready.resolve();
        }, err => {
            this._ready.reject(err);
        });
    }

    addAlias(alias: LanguageModelAlias): void {
        const idx = this.aliases.findIndex(a => a.id === alias.id);
        if (idx !== -1) {
            this.aliases[idx] = alias;
        } else {
            this.aliases.push(alias);
        }
        this.saveToPreference();
        this.onDidChangeEmitter.fire();
    }

    removeAlias(id: string): void {
        const idx = this.aliases.findIndex(a => a.id === id);
        if (idx !== -1) {
            this.aliases.splice(idx, 1);
            this.saveToPreference();
            this.onDidChangeEmitter.fire();
        }
    }

    getAliases(): LanguageModelAlias[] {
        return [...this.aliases];
    }

    resolveAlias(id: string): string[] | undefined {
        const alias = this.aliases.find(a => a.id === id);
        if (!alias) {
            return undefined;
        }
        if (alias.selectedModelId) {
            return [alias.selectedModelId];
        }
        return alias.defaultModelIds;
    }

    /**
     * Set the selected model for the given alias id.
     * Updates the alias' selectedModelId to the given modelId, persists, and fires onDidChange.
     */
    selectModelForAlias(aliasId: string, modelId: string): void {
        const alias = this.aliases.find(a => a.id === aliasId);
        if (alias) {
            alias.selectedModelId = modelId;
            this.saveToPreference();
            this.onDidChangeEmitter.fire();
        }
    }

    /**
     * Load aliases from the persisted setting
     */
    protected loadFromPreference(): void {
        const stored = this.aiConfigurationService.get<{ [name: string]: { selectedModel: string } }>(LANGUAGE_MODEL_ALIASES_PREFERENCE) || {};
        this.aliases.forEach(alias => {
            if (stored[alias.id] && stored[alias.id].selectedModel) {
                alias.selectedModelId = stored[alias.id].selectedModel;
            } else {
                delete alias.selectedModelId;
            }
        });
    }

    /**
     * Persist the current aliases and their selected models to the setting
     */
    protected saveToPreference(): void {
        const map: { [name: string]: { selectedModel: string } } = {};
        for (const alias of this.aliases) {
            if (alias.selectedModelId) {
                map[alias.id] = { selectedModel: alias.selectedModelId };
            }
        }
        this.aiConfigurationService.set(LANGUAGE_MODEL_ALIASES_PREFERENCE, map, PreferenceScope.User);
    }
}
