# Copyright (c) 2026 Airbyte, Inc., all rights reserved.
"""`airbyte-cloud destinations` commands.

## CLI reference

The commands below are regenerated by `poe docs-generate` via cyclopts's
programmatic docs API; see `docs/generate_cli.py`.

.. include:: ../../../docs/generated/airbyte-cloud-cli/destinations.md
   :start-line: 2
"""

from __future__ import annotations

from pathlib import Path
from typing import Annotated

from airbyte import cloud
from airbyte.cloud import connectors as cloud_connectors
from cyclopts import Parameter

from airbyte_cloud_cli._base import App, app
from airbyte_cloud_cli._input import (
    ClientIdArg,
    ClientSecretArg,
    DestinationIdArg,
    PositionalIdArg,
    PublicApiRootArg,
    WorkspaceIdArg,
    parse_config_options,
    resolve_entity_id,
)
from airbyte_cloud_cli._output import json_output

destinations_app = App(name="destinations", help="Manage Airbyte Cloud destinations.")
app.command(destinations_app)


def _workspace(
    *,
    workspace_id: str | None,
    client_id: str | None,
    client_secret: str | None,
    public_api_root: str | None,
) -> cloud.CloudWorkspace:
    return cloud.CloudWorkspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        api_root=public_api_root,
    )


def _destination_info(destination: cloud_connectors.CloudDestination):
    return destination._fetch_connector_info()


@destinations_app.command(name="list")
def list_(
    *,
    workspace_id: WorkspaceIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """List destinations in the workspace."""
    workspace = _workspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        public_api_root=public_api_root,
    )
    json_output(
        [
            _destination_info(destination)
            for destination in workspace.list_destinations()
        ]
    )


@destinations_app.command
def describe(
    *destination_id_args: PositionalIdArg,
    destination_id: DestinationIdArg = None,
    workspace_id: WorkspaceIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """Describe a specific destination."""
    resolved_destination_id = resolve_entity_id(
        destination_id_args,
        destination_id,
        option_name="--destination-id",
    )
    workspace = _workspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        public_api_root=public_api_root,
    )
    json_output(_destination_info(workspace.get_destination(resolved_destination_id)))


@destinations_app.command
def create(
    *,
    name: Annotated[str, Parameter(help="Display name for the destination.")],
    destination_type: Annotated[str, Parameter(help="The destination connector type.")],
    config_file: Annotated[
        Path | None,
        Parameter(help="JSON or YAML connector config file."),
    ] = None,
    config_json: Annotated[
        str | None,
        Parameter(help="Inline JSON connector config object."),
    ] = None,
    workspace_id: WorkspaceIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """Create a new destination in the workspace."""
    config = parse_config_options(config_json=config_json, config_file=config_file)
    config["destinationType"] = destination_type
    workspace = _workspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        public_api_root=public_api_root,
    )
    json_output(
        _destination_info(workspace.deploy_destination(name=name, destination=config))
    )


@destinations_app.command
def rename(
    *destination_id_args: PositionalIdArg,
    name: Annotated[str, Parameter(help="New display name for the destination.")],
    destination_id: DestinationIdArg = None,
    workspace_id: WorkspaceIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """Rename a destination."""
    resolved_destination_id = resolve_entity_id(
        destination_id_args,
        destination_id,
        option_name="--destination-id",
    )
    workspace = _workspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        public_api_root=public_api_root,
    )
    json_output(
        _destination_info(
            workspace.get_destination(resolved_destination_id).rename(name)
        )
    )


@destinations_app.command
def update(
    *destination_id_args: PositionalIdArg,
    destination_id: DestinationIdArg = None,
    config_file: Annotated[
        Path | None,
        Parameter(help="JSON or YAML connector config file."),
    ] = None,
    config_json: Annotated[
        str | None,
        Parameter(help="Inline JSON connector config object."),
    ] = None,
    workspace_id: WorkspaceIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """Update a destination configuration."""
    resolved_destination_id = resolve_entity_id(
        destination_id_args,
        destination_id,
        option_name="--destination-id",
    )
    config = parse_config_options(config_json=config_json, config_file=config_file)
    workspace = _workspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        public_api_root=public_api_root,
    )
    json_output(
        _destination_info(
            workspace.get_destination(resolved_destination_id).update_config(config)
        )
    )


@destinations_app.command
def delete(
    *destination_id_args: PositionalIdArg,
    destination_id: DestinationIdArg = None,
    force: Annotated[
        bool,
        Parameter(help="Skip delete safety checks."),
    ] = False,
    workspace_id: WorkspaceIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """Delete a destination."""
    resolved_destination_id = resolve_entity_id(
        destination_id_args,
        destination_id,
        option_name="--destination-id",
    )
    workspace = _workspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        public_api_root=public_api_root,
    )
    workspace.permanently_delete_destination(
        destination=workspace.get_destination(resolved_destination_id),
        safe_mode=not force,
    )
    json_output({"deleted": True, "destination_id": resolved_destination_id})
