#!/usr/bin/env python3
# -S uv run --script
# /// script
# dependencies = [
#   "click>=8.1.3",
#   "ebooklib>=0.18.0",
#   "epub2audio>=0.1.0",
# ]
# ///
#MISE alias="read-epub"
#MISE description="Read a EPUB file"
"""Read a EPUB file."""

import sys

from pathlib import Path

import click
import ebooklib  # type: ignore
from ebooklib import epub
from loguru import logger

from epub2audio.epub_processor import EpubProcessor


@click.command()
@click.argument("path", type=click.Path(exists=True), default="tests/data/sample.epub")
@click.option("--output", type=click.Path(), default="tests/data/output.txt")
@click.option("--debug", is_flag=True)
def read_epub(path: Path, output: Path, debug: bool) -> epub.EpubBook:
    """Read a EPUB file carefully.

    Meant to be used for debugging.

    Args:
        path: Path to the EPUB file
        output: Path to the output file
        debug: Whether to run in debug mode

    Returns:
        epub.EpubBook: The EPUB book
    """
    logger.remove()
    if debug:
        logger.add(sys.stderr, level="TRACE")
    else:
        logger.add(sys.stderr, level="INFO")
    # from pdb import set_trace; set_trace()
    epub_processor = EpubProcessor(path)

    with open(output, "w") as f:
        f.write(str(epub_processor.book))


if __name__ == "__main__":
    read_epub()
