"""Redsun main entry point."""

from __future__ import annotations

import argparse
import logging
import sys

from redsun.containers import AppContainer


class RedSunArgs(argparse.Namespace):
    """Type hints for command line arguments."""

    config: str
    debug: bool


def parse_args() -> RedSunArgs:
    """Parse command line arguments.

    Returns
    -------
    RedSunArgs
        Parsed command line arguments.
    """
    parser = argparse.ArgumentParser(
        description="Redsun: event-driven data acquisition system"
    )
    parser.add_argument(
        "-c",
        "--config",
        type=str,
        help="Absolute path to the YAML configuration file",
        required=True,
    )
    parser.add_argument(
        "-d",
        "--debug",
        action="store_true",
        help="Enable debug logging",
        default=False,
    )

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args: RedSunArgs = parser.parse_args(namespace=RedSunArgs())
    return args


def main(input_config: str) -> None:
    """Redsun application entry point.

    Parameters
    ----------
    input_config : str
        Path to the configuration file.
    """
    AppContainer.from_config(input_config).run()


def main_cli() -> None:
    """Command line entry point for the Redsun application."""
    args = parse_args()
    logger = logging.getLogger("redsun")
    if args.debug:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)
    main(args.config)


if __name__ == "__main__":
    main_cli()
