# pyright: reportPrivateUsage=false
from typing import Optional

from backend.pashub_fetcher.core_files import CoreFiles
from backend.pashub_fetcher.evidence_file_data import EvidenceFileData
from backend.pashub_fetcher.pashub_client import PashubClient


def make_client() -> PashubClient:
    return PashubClient(token="test-token")


def make_file(
    file_name: str = "unknown.pdf",
    evidence_category: Optional[str] = None,
    created_utc: str = "2024-01-01T00:00:00",
) -> EvidenceFileData:
    return EvidenceFileData(
        file_id="id-1",
        file_name=file_name,
        created_utc=created_utc,
        file_size=1024,
        file_extension="pdf",
        evidence_category=evidence_category,
    )


# ---------------------------------------------------------------------------
# _select_latest_core_files
# ---------------------------------------------------------------------------


def test_select_latest_core_files_returns_single_retrofit_design_doc() -> None:
    # Arrange
    client = make_client()
    files = [
        make_file(
            file_name="2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf",
            evidence_category="retrofit design",
            created_utc="2024-06-01T00:00:00",
        )
    ]

    # Act
    result = client._select_latest_core_files(files)

    # Assert
    assert result[CoreFiles.RETROFIT_DESIGN_DOC].file_name == "2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf"


def test_select_latest_core_files_osm_candidate_wins_over_non_osm() -> None:
    # Arrange - the non-OSM file is newer but should lose to the OSM file
    client = make_client()
    files = [
        make_file(
            file_name="2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf",
            evidence_category="retrofit design",
            created_utc="2024-01-01T00:00:00",
        ),
        make_file(
            file_name="Retrofit Design Doc non-osm variant.pdf",
            evidence_category="retrofit design",
            created_utc="2024-06-01T00:00:00",
        ),
    ]

    # Act
    result = client._select_latest_core_files(files)

    # Assert
    assert result[CoreFiles.RETROFIT_DESIGN_DOC].file_name == "2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf"


def test_select_latest_core_files_picks_latest_when_both_candidates_have_osm() -> None:
    # Arrange
    client = make_client()
    files = [
        make_file(
            file_name="2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf",
            evidence_category="retrofit design",
            created_utc="2024-01-01T00:00:00",
        ),
        make_file(
            file_name="2603-OSM-B06M901-XX-DR-N-A_Alvaston Walk 022.pdf",
            evidence_category="retrofit design",
            created_utc="2024-06-01T00:00:00",
        ),
    ]

    # Act
    result = client._select_latest_core_files(files)

    # Assert
    assert result[CoreFiles.RETROFIT_DESIGN_DOC].file_name == "2603-OSM-B06M901-XX-DR-N-A_Alvaston Walk 022.pdf"


def test_select_latest_core_files_falls_back_to_latest_when_no_osm_candidates() -> None:
    # Arrange
    client = make_client()
    files = [
        make_file(
            file_name="retrofit_design_v1.pdf",
            evidence_category="retrofit design",
            created_utc="2024-01-01T00:00:00",
        ),
        make_file(
            file_name="retrofit_design_v2.pdf",
            evidence_category="retrofit design",
            created_utc="2024-06-01T00:00:00",
        ),
    ]

    # Act
    result = client._select_latest_core_files(files)

    # Assert
    assert result[CoreFiles.RETROFIT_DESIGN_DOC].file_name == "retrofit_design_v2.pdf"
