# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib.metadata
import sys

import click
from rich.console import Console

from .commands.create import create
from .commands.enhance import enhance
from .commands.extract import extract
from .commands.list import list_agents
from .commands.register_gemini_enterprise import register_gemini_enterprise
from .commands.setup_cicd import setup_cicd
from .commands.upgrade import upgrade
from .utils import display_update_message

console = Console()


def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> None:
    if not value or ctx.resilient_parsing:
        return
    try:
        version_str = importlib.metadata.version("agent-starter-pack")
        console.print(f"GCP Agent Starter Pack CLI version: {version_str}")
    except importlib.metadata.PackageNotFoundError:
        console.print("GCP Agent Starter Pack CLI (development version)")
    ctx.exit()


@click.group(help="Production-ready Generative AI Agent templates for Google Cloud")
@click.option(
    "--version",
    "-v",
    is_flag=True,
    callback=print_version,
    expose_value=False,
    is_eager=True,
    help="Show the version and exit.",
)
def cli() -> None:
    # Check for updates at startup (skip if --agent-garden, -ag, or --locked is used)
    if not any(flag in sys.argv for flag in ("--agent-garden", "-ag", "--locked")):
        display_update_message()


# Register commands
cli.add_command(create)
cli.add_command(enhance)
cli.add_command(extract)
cli.add_command(register_gemini_enterprise)
cli.add_command(setup_cicd)
cli.add_command(upgrade)
cli.add_command(list_agents, name="list")


if __name__ == "__main__":
    cli()
