"""
Copyright © 2025-2026 Omnissa, LLC.
"""

import json
import sys

import click
import questionary

from hcs_core.ctxp import cli_options, cli_processor, panic, profile, util


@click.group(name="profile", cls=cli_processor.LazyGroup)
def profile_cmd_group():
    """Commands for profile."""


@profile_cmd_group.command()
def list():
    """List all profile names."""
    return profile.names()


@profile_cmd_group.command()
@click.argument("name", required=False)
def use(name: str):
    """Switch to a specific profile. If no name specified, launch interactive list to choose profile."""

    if name:
        p = profile.use(name)
        if p is None:
            panic("No such profile: " + name)
        return profile.get(name)
    else:
        current = profile.name()
        choices = profile.names()
        if not choices:
            panic("No profile available.")

        ret = questionary.select("Select profile", choices, default=current, show_selected=True).ask()
        if ret:
            if profile.use(ret) is None:
                panic("No such profile: " + name)
            return profile.current()
        else:
            # aborted
            return "", 1


@profile_cmd_group.command()
@click.option("--from-name", "-f", required=False)
@click.option("--to-name", "-t", required=True)
@click.option("--no-edit", "-e", required=False, is_flag=True, help="Edit the profile after copying.")
def copy(from_name: str, to_name: str, no_edit: bool):
    """Copy profile."""

    if not from_name:
        from_name = profile.name()

    data = profile.get(from_name)
    if not data:
        panic("No such profile: " + from_name)
    if profile.exists(to_name):
        panic("Profile already exists: " + to_name)
    data = profile.create(to_name, data, True)

    if no_edit:
        return data
    util.launch_text_editor(profile.file(to_name))


@profile_cmd_group.command()
@click.argument("name", required=False)
def get(name: str):
    """Get content of a specific profile."""
    if name:
        data = profile.get(name)
        if data is None:
            panic("Profile not found. Use 'hcs profile list' to show available profiles, or 'hcs profile init' to create one.")
    else:
        data = profile.current()
        if data is None:
            panic(
                "Default profile not set. Use 'hcs profile list' to show available profiles, 'hcs profile user <name>' to switch to a profile, or 'hcs profile init' to create one."
            )

    return data


@profile_cmd_group.command()
@cli_options.confirm
@click.argument("name", required=False)
def delete(confirm: bool, name: str):
    """Delete a profile by name."""
    if not name:
        name = profile.name()

    if not profile.exists(name):
        return
    if not confirm:
        question = f"Are you sure to delete profile '{name}'?"
        if not questionary.confirm(question, default=False).ask():
            click.echo("Aborted")
            return
    profile.delete(name)


@profile_cmd_group.command()
@click.argument("name", required=False)
def file(name: str):
    """Show file location of a profile by name."""
    if not name:
        name = profile.name()
    return profile.file(name)


@profile_cmd_group.command()
@click.argument("name", type=str)
@click.option(
    "--file",
    "-f",
    type=click.File("rt"),
    default=sys.stdin,
    help="Specify the template file name. If not specified, STDIN will be used.",
)
def create(name: str, file):
    """Create a profile from file or STDIN."""
    with file:
        text = file.read()
    try:
        data = json.loads(text)
    except Exception as e:
        panic(f"Invalid json {e}")

    profile.create(name, data)


@profile_cmd_group.command()
def name():
    """Get current profile name."""
    return profile.name()


@profile_cmd_group.command()
@click.argument("name", type=str, required=False)
def edit(name: str):
    """Launch platform-specific editor to edit the profile file."""

    if not name:
        name = profile.name()
    file_name = profile.file(name)
    util.launch_text_editor(file_name)
