#! /usr/bin/env python

import pathlib
from typing import Annotated

import yuio.app
import yuio.config
import yuio.io
import yuio.parse


class Config(yuio.config.Config):
    """global options

    global options are loaded from `./app_config.json`,
    from environment variables, and from CLI arguments.

    """

    #: number of threads to use for executing model
    threads: Annotated[int, yuio.parse.Ge(1)] = 4

    #: enable or disable gpu
    gpu: bool = True


# We will load values into this variable once the program starts.
CONFIG = Config()


@yuio.app.app
def main(
    config: Config = yuio.config.inline(),
):
    """some ml stuff idk im not into ml"""

    # Load global config:

    # From file...
    config_file = pathlib.Path(__file__).parent / "app_config.json"
    CONFIG.update(Config.load_from_json_file(config_file))

    # From environment variables...
    CONFIG.update(Config.load_from_env("YUIO"))

    # From CLI arguments...
    CONFIG.update(config)

    yuio.io.info("global config is loaded: `%r`", CONFIG)


main.epilog = """
# formatting:

prolog is formatted using the standard markdown. All markdown blocks are supported.
Inline markup is not, though: you can only use backticks and color tags.

Example of what we can do:

- quotes:

  > Beautiful python\v
  > Explicit and simple form\v
  > Winding through clouds
  >
  >     -- from heroku art

- code blocks:

  ```py
  for i in range(10):
      print(f"Hello, {i}!")
  ```

- color tags are supported, as well as `backticks`! Btw, backticks highlight
  `--flags` with appropriate color too!

"""


@main.subcommand(aliases=["r"])
def run(
    #: trained model to execute
    model: pathlib.Path = yuio.config.positional(),
    #: input data for the model
    data: pathlib.Path = yuio.config.positional(),
):
    """apply trained model to a dataset."""

    yuio.io.info("applying model `%s` to data `%s`", model, data)


@main.subcommand(aliases=["t"])
def train(
    #: input data for the model
    data: pathlib.Path = yuio.config.positional(),
    #: output data for the model
    output: pathlib.Path = yuio.config.field(
        default=pathlib.Path("trained.model"),
        flags=["-o", "--output"],
    ),
):
    """train model on a dataset."""

    yuio.io.info("training model `%s` on data `%s`", output, data)


if __name__ == "__main__":
    main.run()
