from __future__ import annotations

from pathlib import Path

import pytest

from pdftomd.options import ConversionOptions, FormulaParser, RuntimeMode


def test_conversion_options_defaults_match_project_policy() -> None:
    options = ConversionOptions()

    assert options.runtime is RuntimeMode.CUDA
    assert options.formula_parser is FormulaParser.NOUGAT
    assert options.chunk_target_pages == 20
    assert options.output_dir == Path("output")
    assert options.resume is False
    assert options.write_logs is True


def test_runtime_modes_express_cuda_fail_fast_and_auto_fallback() -> None:
    assert RuntimeMode.CUDA.requires_cuda
    assert not RuntimeMode.CUDA.allows_cpu_fallback
    assert RuntimeMode.AUTO.allows_cpu_fallback
    assert not RuntimeMode.CPU.requires_cuda


def test_conversion_options_validate_chunk_size_and_formula_boundary() -> None:
    with pytest.raises(ValueError, match="chunk_target_pages"):
        ConversionOptions(chunk_target_pages=0)

    options = ConversionOptions(formula_parser=FormulaParser.MARKER)
    assert options.formula_parser is FormulaParser.MARKER
    assert not hasattr(options, "pyqt")
    assert not hasattr(options, "api_url")


def test_conversion_options_normalize_optional_paths(tmp_path: Path) -> None:
    options = ConversionOptions(
        output_dir=tmp_path / "out",
        nougat_command=tmp_path / "venv" / "Scripts" / "nougat.exe",
        model_cache_dir=tmp_path / ".models",
        log_dir=tmp_path / "logs",
    )

    assert options.output_dir == tmp_path / "out"
    assert options.nougat_command is not None
    assert options.model_cache_dir == tmp_path / ".models"
    assert options.log_dir == tmp_path / "logs"
