# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import os
from urllib.parse import urlparse

import click
from openai import OpenAI
from rich.console import Console
from rich.table import Table

from ..load_provider_confs import get_available_models, load_provider_configs


@click.command(name="list-models")
@click.argument(
    "provider_name_or_url",
    type=str,
    required=True,
)
def list_models_command(provider_name_or_url):
    """
    List all available models for a provider. For self-hosted providers,
    models are queried dynamically. For others, the list is derived from the static YAML configs.
    """

    available_models = get_available_models()
    console = Console()
    models_list = []
    try:
        url_info = urlparse(provider_name_or_url)
        is_url = bool(url_info.netloc)
    except Exception:
        is_url = False

    self_hosted = False
    provider_configs = load_provider_configs()
    config = None
    if not is_url:
        if provider_name_or_url not in provider_configs:
            console.print(f"[bold red]Error:[/] Provider '[cyan]{provider_name_or_url}[/]' not found.")
            return
        config = provider_configs[provider_name_or_url]
        self_hosted = config.self_hosted

    if is_url or self_hosted:
        if self_hosted:
            base_url = config.base_url
            api_key = os.getenv(config.api_key_var)
        else:
            base_url = provider_name_or_url
            api_key = os.getenv("OPENAI_API_KEY")

        try:
            client = OpenAI(
                api_key=api_key,
                base_url=base_url,
            )
            models_list = [model.id for model in client.models.list()]
        except Exception as e:
            console.print(f"[bold red]Error connecting to OpenAI compatible endpoint {base_url}: {e}[/]")
            return
    else:
        for (p, model_id), _ in available_models.items():
            if p == provider_name_or_url:
                models_list.append(model_id)

    table = Table()
    table.add_column("Model Name", style="cyan")

    models_list.sort()

    for model_id in models_list:
        table.add_row(model_id)

    console.print(table)
