# Copyright (c) 2026 Airbyte, Inc., all rights reserved.
"""`airbyte-cloud workspaces` 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/workspaces.md
   :start-line: 2
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from airbyte import cloud
from airbyte._util import api_util
from airbyte.constants import CLOUD_API_ROOT
from airbyte.secrets import SecretString

if TYPE_CHECKING:
    from airbyte.cloud import CloudWorkspace

from airbyte_cloud_cli._base import App, app
from airbyte_cloud_cli._input import (
    ClientIdArg,
    ClientSecretArg,
    OrganizationIdArg,
    PublicApiRootArg,
    WorkspaceIdArg,
)
from airbyte_cloud_cli._output import json_output

workspaces_app = App(name="workspaces", help="Manage Airbyte Cloud workspaces.")
app.command(workspaces_app)


def _workspace_info(workspace: CloudWorkspace):
    return workspace.get_info()


def _auth_args(
    *,
    client_id: str | None,
    client_secret: str | None,
) -> tuple[SecretString | None, SecretString | None, None]:
    return (
        SecretString(client_id) if client_id is not None else None,
        SecretString(client_secret) if client_secret is not None else None,
        None,
    )


@workspaces_app.command(name="list")
def list_(
    *,
    organization_id: OrganizationIdArg = None,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """List workspaces in an organization."""
    resolved_public_api_root = (
        CLOUD_API_ROOT if public_api_root is None else public_api_root
    )
    resolved_client_id, resolved_client_secret, bearer_token = _auth_args(
        client_id=client_id,
        client_secret=client_secret,
    )
    json_output(
        api_util.list_workspaces_in_organization(
            organization_id=organization_id,
            api_root=resolved_public_api_root,
            client_id=resolved_client_id,
            client_secret=resolved_client_secret,
            bearer_token=bearer_token,
        )
    )


@workspaces_app.command
def describe(
    workspace_id: WorkspaceIdArg = None,
    *,
    client_id: ClientIdArg = None,
    client_secret: ClientSecretArg = None,
    public_api_root: PublicApiRootArg = None,
) -> None:
    """Describe workspace details."""
    workspace = cloud.CloudWorkspace(
        workspace_id=workspace_id,
        client_id=client_id,
        client_secret=client_secret,
        api_root=public_api_root,
    )
    json_output(_workspace_info(workspace))
