// *****************************************************************************
// Copyright (C) 2026 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 * as React from '@theia/core/shared/react';
import { nls } from '@theia/core';
import { HoverService } from '@theia/core/lib/browser';
import { GenericCapabilitySelections } from '@theia/ai-core';
import { AvailableGenericCapabilities } from './generic-capabilities-service';
import { GenericCapabilitiesTree, ServerToolsSection } from './generic-capabilities-tree';

export interface GenericCapabilitiesSectionProps {
    /** Current generic capability selections */
    genericCapabilities: GenericCapabilitySelections;
    /** Called when a capability type's selection changes */
    onGenericCapabilityChange: (type: keyof GenericCapabilitySelections, ids: string[]) => void;
    /** Called to reset all generic capabilities to stored defaults */
    onResetGenericCapabilities: () => void;
    /** Available capabilities to select from */
    availableCapabilities: AvailableGenericCapabilities;
    /** Items already in the agent prompt that should be disabled/greyed */
    disabledCapabilities: GenericCapabilitySelections;
    /** Optional server tools section for the current model's provider */
    serverTools?: ServerToolsSection;
    /** Whether the section is disabled */
    disabled?: boolean;
    /** Hover service for tooltips */
    hoverService: HoverService;
}

/**
 * Section component displaying generic capabilities in a tree layout.
 * Shows a unified multi-level tree with all capability types as roots,
 * groups as intermediate nodes, and items as leaves.
 * Includes a search bar for filtering capabilities.
 */
export const GenericCapabilitiesSection: React.FunctionComponent<GenericCapabilitiesSectionProps> = ({
    genericCapabilities,
    onGenericCapabilityChange,
    onResetGenericCapabilities,
    availableCapabilities,
    disabledCapabilities,
    serverTools,
    disabled,
    hoverService
}) => {
    // Check if we have any available capabilities or server tools to show
    const hasAvailableCapabilities =
        availableCapabilities.skills.length > 0 ||
        availableCapabilities.mcpFunctions.length > 0 ||
        availableCapabilities.functions.length > 0 ||
        availableCapabilities.promptFragments.length > 0 ||
        availableCapabilities.agentDelegation.length > 0 ||
        availableCapabilities.variables.length > 0 ||
        (serverTools !== undefined && serverTools.tools.length > 0);

    if (!hasAvailableCapabilities) {
        return undefined;
    }

    return (
        <div className="theia-ChatInput-GenericCapabilitiesSection">
            <div className="theia-ChatInput-GenericCapabilitiesSection-heading">
                {nls.localize('theia/ai/chat-ui/genericCapabilities', 'Generic Capabilities')}
            </div>
            <GenericCapabilitiesTree
                genericCapabilities={genericCapabilities}
                onGenericCapabilityChange={onGenericCapabilityChange}
                onResetGenericCapabilities={onResetGenericCapabilities}
                availableCapabilities={availableCapabilities}
                disabledCapabilities={disabledCapabilities}
                serverTools={serverTools}
                disabled={disabled}
                hoverService={hoverService}
            />
        </div>
    );
};
