from backend.pashub_fetcher.core_files import (
    CoreFiles,
    get_core_file_type,
    get_file_type_string,
)


def test_file_type_for_photopack():
    assert get_file_type_string("Photopack_123456_V1.pdf") == "photo_pack"


def test_file_type_for_sitenote():
    assert get_file_type_string("SiteNote_123456_V1.pdf") == "site_note"


def test_file_type_for_rdsap_sitenote():
    assert (
        get_file_type_string("RdSAP_SiteNote_9510890_V1_Assessmet.pdf")
        == "rd_sap_site_note"
    )


def test_file_type_for_pas2023_ventilation():
    assert (
        get_file_type_string("PAS 2023 Ventilation Assessment Report_123456.pdf")
        == "pas_2023_ventilation"
    )


def test_file_type_for_pas2023_condition():
    assert (
        get_file_type_string("PAS 2023 Condition Report_123456.pdf")
        == "pas_2023_condition"
    )


def test_file_type_for_pas_significance():
    assert get_file_type_string("PAS Significance_123456.pdf") == "pas_significance"


def test_file_type_for_par_photopack():
    assert (
        get_file_type_string("PAR Photo Pack_95101890_V2_Assessment.pdf")
        == "par_photo_pack"
    )


def test_file_type_for_pas2023_property():
    assert (
        get_file_type_string("PAS 2023 Property Assessment Report_123456.pdf")
        == "pas_2023_property"
    )


def test_file_type_for_pas2023_occupancy():
    assert (
        get_file_type_string("PAS 2023 Occupancy Assessment Report_123456.pdf")
        == "pas_2023_occupancy"
    )


def test_file_type_for_improvement_option_evaluation():
    # filename: "{job_id} - {postcode} - Improvement Option Evaluation.pdf"
    assert (
        get_file_type_string("6000802 - NG4 4HD - Improvement Option Evaluation.pdf")
        == "improvement_option_evaluation"
    )


def test_file_type_for_medium_term_improvement_plan():
    # filename: "{job_id} - {postcode} - Medium Term Improvement Plan IOE.pdf"
    assert (
        get_file_type_string(
            "60800802 - NG4 4HD - Medium Term Improvement Plan IOE.pdf"
        )
        == "medium_term_improvement_plan"
    )


def test_file_type_for_retrofit_design_doc():
    assert (
        get_file_type_string("2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf")
        == "retrofit_design_doc"
    )
    assert (
        get_file_type_string("2603-OSM-B06M901-XX-DR-N-A_Alvaston Walk 022.pdf")
        == "retrofit_design_doc"
    )


# ---------------------------------------------------------------------------
# core_file_for
# ---------------------------------------------------------------------------


def test_core_file_for_evidence_category_match_is_case_insensitive() -> None:
    # Arrange
    filename = "2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf"

    # Act
    result = get_core_file_type(filename, evidence_category="Retrofit Design")

    # Assert
    assert result == CoreFiles.RETROFIT_DESIGN_DOC


def test_core_file_for_evidence_category_returns_retrofit_design_doc() -> None:
    # Arrange
    filename = "2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf"

    # Act
    result = get_core_file_type(filename, evidence_category="retrofit design")

    # Assert
    assert result == CoreFiles.RETROFIT_DESIGN_DOC


def test_core_file_for_ioe_substring_returns_improvement_option_evaluation() -> None:
    # Arrange
    filename = "6000802 - NG4 4HD - Improvement Option Evaluation.pdf"

    # Act
    result = get_core_file_type(filename)

    # Assert
    assert result == CoreFiles.IMPROVEMENT_OPTION_EVALUATION


def test_core_file_for_mtip_substring_returns_medium_term_improvement_plan() -> None:
    # Arrange
    filename = "60800802 - NG4 4HD - Medium Term Improvement Plan IOE.pdf"

    # Act
    result = get_core_file_type(filename)

    # Assert
    assert result == CoreFiles.MEDIUM_TERM_IMPROVEMENT_PLAN


def test_core_file_for_osm_pattern_returns_retrofit_design_doc_without_evidence_category() -> (
    None
):
    # Arrange
    filename = "2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf"

    # Act
    result = get_core_file_type(filename)

    # Assert
    assert result == CoreFiles.RETROFIT_DESIGN_DOC


def test_core_file_for_prefix_returns_photopack() -> None:
    # Arrange
    filename = "Photopack_123456_V1.pdf"

    # Act
    result = get_core_file_type(filename)

    # Assert
    assert result == CoreFiles.PHOTOPACK


def test_core_file_for_unknown_filename_returns_none() -> None:
    # Arrange
    filename = "unknown_document_123.pdf"

    # Act
    result = get_core_file_type(filename)

    # Assert
    assert result is None


def test_core_file_for_osm_fallback_does_not_fire_when_evidence_category_present() -> (
    None
):
    # Arrange — OSM+DR-N-A filename but evidence_category is something other than retrofit design
    filename = "2512-OSM-H21M900-XX-DR-N-A_Lord Nelson Street 018.pdf"

    # Act
    result = get_core_file_type(filename, evidence_category="some other category")

    # Assert
    assert result is None
