from pathlib import Path

import click

from common.z3_utils import set_pretty_print
from problems.cli_utils import problem_options

from . import mark
from .problem import Problem


@click.group()
def main() -> None: ...


@main.command(name="export-smt")
@click.argument(
    "output_directory",
    type=click.Path(file_okay=False, dir_okay=True, writable=True, path_type=Path),
    required=False,
)
@problem_options
def export_smt(problems: set[Problem], output_directory: Path | None) -> None:
    set_pretty_print()

    if output_directory is not None:
        output_directory.mkdir(parents=True, exist_ok=True)

    for problem in problems:
        if output_directory is None:
            click.echo(problem.to_smt())
        else:
            (output_directory / problem.name).with_suffix(".smt2").write_text(
                problem.to_smt()
            )


@main.command()
@problem_options
def count(problems: set[Problem]) -> None:
    click.echo(f"Found {len(problems)} problems")


@main.command(name="list")
@problem_options
def list_problems(problems: set[Problem]) -> None:
    for problem in sorted(problems):
        click.echo(problem.name)


@main.command(name="markers")
def list_markers() -> None:
    for marker in mark.markers():
        click.echo(marker)


if __name__ == "__main__":
    main()
