"""Tests for table parsing."""
import pytest

from app.table_extraction.parser import table_parser


class TestTableParsing:
    """Tests for maintenance table parsing."""

    def test_parse_simple_table(self) -> None:
        """Test parsing a simple maintenance table."""
        header = ["Service", "Miles", "Months"]
        data = [
            ["Engine Oil", "5,000", "6"],
            ["Air Filter", "30,000", "24"],
            ["Cabin Filter", "15,000", "12"],
        ]

        results = table_parser.parse_table(header, data)

        assert len(results) == 3

        # Check oil change
        oil = next(r for r in results if "oil" in r.service.lower())
        assert oil.interval_miles == 5000
        assert oil.interval_months == 6

    def test_parse_table_with_notes(self) -> None:
        """Test parsing table with notes column."""
        header = ["Item", "Interval", "Notes"]
        data = [
            ["Engine Oil", "5,000 miles or 6 months", "Use 0W-20"],
            ["Brake Fluid", "30,000 miles", "DOT 4"],
        ]

        results = table_parser.parse_table(header, data)

        assert len(results) == 2

    def test_parse_without_headers(self) -> None:
        """Test parsing table without clear headers."""
        data = [
            ["Engine oil change", "5,000 miles", "6 months"],
            ["Tire rotation", "7,500 miles", ""],
        ]

        results = table_parser._parse_without_headers(data)

        assert len(results) >= 1

    def test_parse_text_block(self) -> None:
        """Test parsing unstructured text."""
        text = """
        Engine oil: replace every 5,000 miles or 6 months
        Air filter: replace every 30,000 miles
        Tire rotation: every 7,500 miles
        """

        results = table_parser.parse_text_block(text)

        assert len(results) >= 2


class TestColumnIdentification:
    """Tests for column type identification."""

    def test_identify_service_column(self) -> None:
        """Test identifying service column."""
        header = ["Service Item", "Miles", "Months"]
        columns = table_parser._identify_columns(header)

        assert columns.get(0) == "service"
        assert columns.get(1) == "miles"
        assert columns.get(2) == "months"

    def test_identify_maintenance_column(self) -> None:
        """Test identifying 'maintenance' as service column."""
        header = ["Maintenance", "Interval", "Notes"]
        columns = table_parser._identify_columns(header)

        assert columns.get(0) == "service"

    def test_identify_details_column(self) -> None:
        """Test identifying details/notes column."""
        header = ["Item", "Miles", "Notes"]
        columns = table_parser._identify_columns(header)

        assert columns.get(2) == "details"


class TestIntervalExtraction:
    """Tests for interval extraction from cells."""

    def test_extract_miles_with_comma(self) -> None:
        """Test extracting miles with comma separator."""
        result = table_parser._extract_miles("5,000")
        assert result == 5000

    def test_extract_miles_without_comma(self) -> None:
        """Test extracting miles without comma."""
        result = table_parser._extract_miles("5000")
        assert result == 5000

    def test_extract_miles_with_unit(self) -> None:
        """Test extracting miles with unit."""
        result = table_parser._extract_miles("5,000 miles")
        assert result == 5000

    def test_extract_miles_k_notation(self) -> None:
        """Test extracting miles with K notation."""
        result = table_parser._extract_miles("5K")
        assert result == 5000

    def test_extract_months(self) -> None:
        """Test extracting months."""
        result = table_parser._extract_months("6")
        assert result == 6

    def test_extract_months_with_unit(self) -> None:
        """Test extracting months with unit."""
        result = table_parser._extract_months("12 months")
        assert result == 12
