from enum import Enum
from typing import Optional

from backend.app.db.models.uploaded_file import FileTypeEnum


class CoreFiles(Enum):
    PHOTOPACK = "Photopack"
    SITENOTE = "SiteNote"
    RDSAP_SITENOTE = "RdSAP_SiteNote"
    PAS2023_VENTILATION = "PAS 2023 Ventilation Assessment Report"
    PAS2023_CONDITION = "PAS 2023 Condition Report"
    PAS_SIGNIFICANCE = "PAS Significance"
    PAR_PHOTOPACK = "PAR Photo Pack"
    PAS2023_PROPERTY = "PAS 2023 Property Assessment Report"
    PAS2023_OCCUPANCY = "PAS 2023 Occupancy Assessment Report"
    IMPROVEMENT_OPTION_EVALUATION = "Improvement Option Evaluation"
    MEDIUM_TERM_IMPROVEMENT_PLAN = "Medium Term Improvement Plan"
    RETROFIT_DESIGN_DOC = "Retrofit Design Doc"


_CORE_FILE_TO_FILE_TYPE: dict[CoreFiles, str] = {
    CoreFiles.PHOTOPACK: FileTypeEnum.PHOTO_PACK.value,
    CoreFiles.SITENOTE: FileTypeEnum.SITE_NOTE.value,
    CoreFiles.RDSAP_SITENOTE: FileTypeEnum.RD_SAP_SITE_NOTE.value,
    CoreFiles.PAS2023_VENTILATION: FileTypeEnum.PAS_2023_VENTILATION.value,
    CoreFiles.PAS2023_CONDITION: FileTypeEnum.PAS_2023_CONDITION.value,
    CoreFiles.PAS_SIGNIFICANCE: FileTypeEnum.PAS_SIGNIFICANCE.value,
    CoreFiles.PAR_PHOTOPACK: FileTypeEnum.PAR_PHOTO_PACK.value,
    CoreFiles.PAS2023_PROPERTY: FileTypeEnum.PAS_2023_PROPERTY.value,
    CoreFiles.PAS2023_OCCUPANCY: FileTypeEnum.PAS_2023_OCCUPANCY.value,
    CoreFiles.IMPROVEMENT_OPTION_EVALUATION: FileTypeEnum.IMPROVEMENT_OPTION_EVALUATION.value,
    CoreFiles.MEDIUM_TERM_IMPROVEMENT_PLAN: FileTypeEnum.MEDIUM_TERM_IMPROVEMENT_PLAN.value,
    CoreFiles.RETROFIT_DESIGN_DOC: FileTypeEnum.RETROFIT_DESIGN_DOC.value,
}


def get_core_file_type(
    filename: str, evidence_category: Optional[str] = None
) -> Optional[CoreFiles]:
    # Identify retrofit design doc using evidence category as the name is possibly unreliable.
    # We might change to always use evidence category, but needs more investigation
    if evidence_category is not None and evidence_category.lower() == "retrofit design":
        return CoreFiles.RETROFIT_DESIGN_DOC

    if CoreFiles.IMPROVEMENT_OPTION_EVALUATION.value in filename:
        return CoreFiles.IMPROVEMENT_OPTION_EVALUATION

    if CoreFiles.MEDIUM_TERM_IMPROVEMENT_PLAN.value in filename:
        return CoreFiles.MEDIUM_TERM_IMPROVEMENT_PLAN

    if evidence_category is None and "-OSM-" in filename and "DR-N-A" in filename:
        return CoreFiles.RETROFIT_DESIGN_DOC

    _prefix_skip = {
        CoreFiles.RETROFIT_DESIGN_DOC,
        CoreFiles.IMPROVEMENT_OPTION_EVALUATION,
        CoreFiles.MEDIUM_TERM_IMPROVEMENT_PLAN,
    }

    for core_file in CoreFiles:
        if core_file in _prefix_skip:
            continue

        if filename.startswith(core_file.value):
            return core_file

    return None


def get_file_type_string(filename: str) -> Optional[str]:
    core_file: Optional[CoreFiles] = get_core_file_type(filename)

    if core_file is None:
        return None

    return _CORE_FILE_TO_FILE_TYPE[core_file]
