import typer
from typing_extensions import Annotated
from typing import Optional

from texmemo.extractor import Extractor
from texmemo.types import parse_section_config

app = typer.Typer()


def extract(
    input: Annotated[str, typer.Argument(
        help="Path to input .tex file or to its parent folder")],
    output: Annotated[str, typer.Argument(
        help="Output file path. This can be a path to a .tex file, or a folder name. The output is a folder unless --file-only is passed."
    )],
    title: Annotated[Optional[str], typer.Option(
        "--title", "-t", help="If given, overrides the title of the input document.")] = None,
    author: Annotated[Optional[str], typer.Option(
        "--author", "-a", help="If given, overrides the author of the input document.")] = None,
    sections: Annotated[Optional[list[str]], typer.Option(
        "--section", "-s",
        help="Name of the environment to be picked out. Can be given multiple times. To specify a child environment, e.g. `proof` pass in theorem:proof.")] = None,
    file_only: Annotated[bool, typer.Option(
        "--file-only", "-F", help="The output .tex file is not wrapped in a folder.")] = False,
    strict_mode: Annotated[bool, typer.Option(
        "--strict", "-S", help="Do not tolerate invalid .tex input."
    )] = False,
    force: Annotated[bool, typer.Option(
        "--force", "-f", help="Ovewrite an existing .tex file."
    )] = False,

):
    parsed_sections = parse_section_config(sections)
    extractor = Extractor()
    extractor.extract(input, output, title, author, parsed_sections, file_only,
                      strict_mode, force, write_file=True, skip_filter=False)
