#
# # Copyright © 2023 Peak AI Limited. or its affiliates. All Rights Reserved.
# #
# # Licensed under the Apache License, Version 2.0 (the "License"). You
# # may not use this file except in compliance with the License. A copy of
# # the License is located at:
# #
# # https://github.com/PeakBI/peak-sdk/blob/main/LICENSE
# #
# # or in the "license" file accompanying this file. This file 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.
# #
# # This file is part of the peak-sdk.
# # see (https://github.com/PeakBI/peak-sdk)
# #
# # You should have received a copy of the APACHE LICENSE, VERSION 2.0
# # along with this program. If not, see <https://apache.org/licenses/LICENSE-2.0>
#
"""Peak deployments commands."""
from typing import List, Optional

import typer
from peak.cli import args
from peak.press.deployments import Deployment
from rich.console import Console

app = typer.Typer(
    help="Manage both Block and App deployments.",
    short_help="Manage both Block and App deployments.",
)
console = Console()


@app.command("list", short_help="List App and Block deployments.")
def list_deployments(
    ctx: typer.Context,
    page_size: Optional[int] = args.PAGE_SIZE,
    page_number: Optional[int] = args.PAGE_NUMBER,
    status: Optional[List[str]] = args.STATUS_FILTER,
    kind: Optional[str] = args.KIND_FILTER,
    term: Optional[str] = args.TERM_FILTER,
    sort: Optional[List[str]] = args.SORT_KEYS,
) -> None:
    """***List*** all the App and Block deployments that have been created for the tenant.

    \b
    📝 ***Example usage:***<br/>
    ```bash
    peak deployments list --page-size 10 --page-number 1
    ```

    \b
    🆗 ***Response:***
    ```
    {
        "deploymentCount": 1,
        "deployments": [...],
        "pageCount": 1,
        "pageNumber": 1,
        "pageSize": 10
    }
    ```

    🔗 [**API Documentation**](https://press.peak.ai/api-docs/index.htm#/Deployments/get_v1_deployments)
    """
    deployment_client: Deployment = ctx.obj["client"]
    response = deployment_client.list_deployments(
        status=status,
        kind=kind,
        term=term,
        sort=sort,
        page_size=page_size,
        page_number=page_number,
        return_iterator=False,
    )
    console.print(response)
