# Copyright NTESS. See COPYRIGHT file for details.
#
# SPDX-License-Identifier: MIT

import argparse
from typing import TYPE_CHECKING

from ...hookspec import hookimpl
from ...workspace import Workspace
from ..types import CanarySubcommand

if TYPE_CHECKING:
    from ...config.argparsing import Parser


@hookimpl
def canary_addcommand(parser: "Parser") -> None:
    parser.add_command(GarbageCollect())


class GarbageCollect(CanarySubcommand):
    name = "gc"
    description = "Garbage collect (remove) files generated by cases with status 'success'"

    def setup_parser(self, parser: "Parser") -> None:
        parser.add_argument(
            "--dryrun",
            action="store_true",
            help="Show what would be removed by garbage collection, but don't perform deletion",
        )

    def execute(self, args: argparse.Namespace) -> int:
        workspace = Workspace.load()
        workspace.gc(dryrun=args.dryrun)
        return 0
