# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Debusine command line interface, asset management commands."""

import argparse
from typing import override

from debusine.assets import AssetCategory, asset_data_model
from debusine.client.commands.base import (
    DebusineCommand,
    RequiredInputDataCommand,
    WorkspaceCommand,
)


class Create(RequiredInputDataCommand, WorkspaceCommand, group="asset"):
    """Create an asset."""

    @override
    @classmethod
    def configure(cls, parser: argparse.ArgumentParser) -> None:
        super().configure(parser)
        parser.add_argument(
            "category",
            type=AssetCategory,
            help="Category of asset",
        )

    @override
    def run(self) -> None:
        category = self.args.category

        asset_data = asset_data_model(category, self.input_data)

        with self._api_call_or_fail():
            asset = self.debusine.asset_create(
                category=category,
                data=asset_data,
                workspace=self.workspace,
            )

        # TODO: Ideally this would show a URL instead like everything
        # else, but assets don't have a web view at the moment.
        self.print_yaml_output(
            {
                "result": "success",
                "message": (
                    f"New asset created in {self.debusine.base_api_url} "
                    f"in workspace {asset.workspace} with id {asset.id}."
                ),
            }
        )


class LegacyCreate(
    Create, name="create-asset", deprecated="see `asset create`"
):
    """Create an asset."""


class List(DebusineCommand, group="asset"):
    """List assets."""

    @override
    @classmethod
    def configure(cls, parser: argparse.ArgumentParser) -> None:
        super().configure(parser)
        parser.add_argument(
            "--id",
            metavar="ID",
            type=int,
            help="Show an individual asset by ID.",
        )
        parser.add_argument(
            "--workspace",
            "-w",
            metavar="NAME",
            type=str,
            help="List all assets in workspace.",
        )
        parser.add_argument(
            "--work-request",
            metavar="ID",
            type=int,
            help="List assets created by work request.",
        )

    @override
    def run(self) -> None:
        asset = self.args.id
        workspace = self.args.workspace
        work_request = self.args.work_request
        if asset is None and workspace is None and work_request is None:
            self._fail(
                "At least one of --id, --workspace, and --work-request must "
                "be specified."
            )
        with self._api_call_or_fail():
            assets = self.debusine.asset_list(
                asset_id=asset,
                work_request=work_request,
                workspace=workspace,
            )

        self.print_yaml_output(assets.model_dump(mode="json"))


class LegacyList(List, name="list-assets", deprecated="see `asset list`"):
    """List assets."""
