"""Tests for LaTeX export."""

from astropydantic import AstroPydanticTime

import ctao_datamodel as dm
from ctao_datamodel._latex import OPTIONAL_SYMBOL, extract_keyword_mapping


class Example1(dm.ModelBase):
    start_time: AstroPydanticTime = dm.AstroField(
        description="test", fits_keyword="TSTART"
    )
    energy: float = dm.AstroField(
        description="reco energy", unit="TeV", ucd="em.energy"
    )
    optional: int | None = dm.AstroField(default=6)


def test_extract_keyword_mapping():
    mapping = extract_keyword_mapping(Example1, "fits_keyword")

    assert mapping["start_time"] == ["TSTART"]


def test_model_to_table():
    table = dm.model_to_table(Example1)

    assert len(table) == len(Example1.model_fields)
    assert "Description" in table.colnames
    assert table[1]["Unit"] == "$\\mathrm{TeV}$"
    assert OPTIONAL_SYMBOL in table[2]["Name"]


def test_model_to_latex_table():
    text = dm.model_to_latex_table(Example1)
    assert len(text) > 0


def test_generate_latex_table_includes(tmp_path):
    dm.generate_latex_table_includes([Example1], tmp_path)
    expected_path = tmp_path / "table_Example1.inc.tex"
    assert expected_path.exists()


def test_generate_latex_table_includes_2(tmp_path, example_model):
    dm.generate_latex_table_includes([example_model], tmp_path)
    expected_path = tmp_path / "table_ExampleModel.inc.tex"
    assert expected_path.exists()
