// *****************************************************************************
// 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 { AICommandHandlerFactory } from '@theia/ai-core/lib/browser/ai-command-handler-factory';
import { CommandContribution, CommandRegistry, MessageService, nls, PreferenceService, ILogger } from '@theia/core';
import { QuickInputService } from '@theia/core/lib/browser';
import { inject, injectable, named } from '@theia/core/shared/inversify';
import { LlamafileManager } from '../common/llamafile-manager';
import { PREFERENCE_LLAMAFILE } from '../common/llamafile-preferences';
import { LlamafileEntry } from './llamafile-frontend-application-contribution';

export const StartLlamafileCommand = {
    id: 'llamafile.start',
    label: nls.localize('theia/ai/llamaFile/start', 'Start Llamafile'),
};
export const StopLlamafileCommand = {
    id: 'llamafile.stop',
    label: nls.localize('theia/ai/llamaFile/stop', 'Stop Llamafile'),
};

@injectable()
export class LlamafileCommandContribution implements CommandContribution {

    @inject(QuickInputService)
    protected readonly quickInputService: QuickInputService;

    @inject(AICommandHandlerFactory)
    protected readonly commandHandlerFactory: AICommandHandlerFactory;

    @inject(PreferenceService)
    protected preferenceService: PreferenceService;

    @inject(MessageService)
    protected messageService: MessageService;

    @inject(LlamafileManager)
    protected llamafileManager: LlamafileManager;

    @inject(ILogger) @named('ai-llamafile:LlamafileCommandContribution')
    protected readonly logger: ILogger;

    registerCommands(commandRegistry: CommandRegistry): void {
        commandRegistry.registerCommand(StartLlamafileCommand, this.commandHandlerFactory({
            execute: async () => {
                try {
                    const llamaFiles = this.preferenceService.get<LlamafileEntry[]>(PREFERENCE_LLAMAFILE);
                    if (llamaFiles === undefined || llamaFiles.length === 0) {
                        this.messageService.error(nls.localize('theia/ai/llamafile/error/noConfigured', 'No Llamafiles configured.'));
                        return;
                    }
                    const options = llamaFiles.map(llamaFile => ({ label: llamaFile.name }));
                    const result = await this.quickInputService.showQuickPick(options);
                    if (result === undefined) {
                        return;
                    }
                    this.llamafileManager.startServer(result.label);
                } catch (error) {
                    this.logger.error('Something went wrong during the llamafile start.', error);
                    this.messageService.error(
                        nls.localize(
                            'theia/ai/llamafile/error/startFailed',
                            'Something went wrong during the llamafile start: {0}.\nFor more information, see the console.',
                            error.message
                        ));
                }
            }
        }));
        commandRegistry.registerCommand(StopLlamafileCommand, this.commandHandlerFactory({
            execute: async () => {
                try {
                    const llamaFiles = await this.llamafileManager.getStartedLlamafiles();
                    if (llamaFiles === undefined || llamaFiles.length === 0) {
                        this.messageService.error(nls.localize('theia/ai/llamafile/error/noRunning', 'No Llamafiles running.'));
                        return;
                    }
                    const options = llamaFiles.map(llamaFile => ({ label: llamaFile }));
                    const result = await this.quickInputService.showQuickPick(options);
                    if (result === undefined) {
                        return;
                    }
                    this.llamafileManager.stopServer(result.label);
                } catch (error) {
                    this.logger.error('Something went wrong during the llamafile stop.', error);
                    this.messageService.error(
                        nls.localize(
                            'theia/ai/llamafile/error/stopFailed',
                            'Something went wrong during the llamafile stop: {0}.\nFor more information, see the console.',
                            error.message
                        ));
                }
            }
        }));
    }
}
