import re

from dataclasses import dataclass
from datetime import datetime
from typing import Optional


@dataclass
class PeabodyAssetCondition:
    lo_reference: str
    full_address: str
    location_type_code: int
    parent_lo_reference: str
    element_code: int
    element: int
    sub_element_code: int
    sub_element: str
    material_code: int
    material_or_answer: str
    renewal_quantity: int
    renewal_year: int
    cloned: str
    lo_type_code: int
    renewal_cost: Optional[float] = None
    condition_survey_date: Optional[datetime] = None

    @property
    def is_block_level(self) -> bool:
        # TODO: maybe use block codes from other Peabody dataset to do this instead

        if not self.full_address:
            return False

        address = self.full_address.upper()

        block_level_patterns = [
            r"\bBLOCK\b",  # BLOCK MILNE HOUSE
            r"\bFLATS\b",  # FLATS A-D
            r"\b\d+[A-Z]?-\d+[A-Z]?\b",  # 1-80, 9A-9H
            r"\b\d+[A-Z]-[A-Z]\b",  # 81A-B
            r"\b\d+\s*&\s*\d+\b",  # 73 & 74
        ]

        return any(re.search(pattern, address) for pattern in block_level_patterns)
