# import extract_msg
from datetime import datetime
from sqlalchemy.orm import sessionmaker
from backend.app.db.connection import db_engine
from backend.app.db.functions.non_intrusive_surveys import upload_non_intrusive_survey_notes


def parse_msg_body(text):
    # Split the text into lines
    lines = text.split('\r\n')

    # Dictionary to hold the parsed data
    data = {}

    # Process each line
    for line in lines:
        # Remove all asterisks and extra whitespace
        clean_line = line.replace('*', '').strip()

        if clean_line:  # Ensure the line is not empty after cleaning
            # Attempt to split clean '=' if present
            if '=' in clean_line:
                clean_line = clean_line.replace(' = ', ': ')

            # Use line content as a key with a default value indicating presence
            # Generate a unique key for lines without '='
            data[f"Info{len(data) + 1}"] = clean_line

    return data


def app():
    """
    This code retrieves the results of the non-invasive surveys, to be stored in S3
    :return:
    """

    # filepath = ("/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/5 Oaklands B62 "
    #             "0JA/Immo - 5 Oaklands Halesowen B62 0JA.msg")
    # filepath = ("/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/6 Beech Rd DY1 "
    #             "4BP/IMMO - 6 Beech Road Dudley DY1 4BP.msg")
    # filepath = (
    #     "/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/8 Corporation Rd DY2 "
    #     "7PX/IMMO - 8 Corporation Road Dudley DY2 7PX.msg"
    # )
    # filepath = (
    #     "/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/21 Wells Rd DY5 3TB/"
    #     "IMMO - 21 Wells Road Brierley Hill DY5 3TB.msg"
    # )
    # filepath = (
    #     "/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/47 Fairfield Rd DY8 "
    #     "5UJ/IMMO - 47 Fairfield Road Wordsley Stourbridge DY8 5UJ.msg"
    # )
    # filepath = (
    #     "/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/91 Osprey Drive DY1 "
    #     "2JS/IMMO - 91 Osprey Drive Dudley DY1 2JS.msg"
    # )
    # filepath = (
    #     "/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/195 Ashenhurst Rd DY1 "
    #     "2JB/IMMO - 195 Ashenhurst Road Dudley DY1 2JB.msg"
    # )
    # filepath = (
    #     "/Users/khalimconn-kowlessar/Downloads/IMMO - Dudley Pilot - non-invasive raw data/27 Milton Rd DY1 2JB/IMMO "
    #     "- 27 Milton Road Coseley Bilston WV14 8HZ.msg"
    # )
    #
    # with extract_msg.Message(filepath) as msg:
    #     body = msg.body
    #
    # from pprint import pprint
    # pprint(parse_msg_body(body))

    # We manually create the non-invasive notes for the pilot
    non_invasive_notes = [
        {
            'uprn': 90028499,
            # 'address': '5 Oaklands',
            # 'postcode': 'B62 0JA',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation. '
                               'There is a shared alleyway with the neighbour, that is a solid brick wall.',
            'Wall Render': 'Partial render between top of ground floor window and bottom of 1st floor window',
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Front house direction: North East, Back house direction: South West',
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90055152,
            # 'address': '6 Beech Road',
            # 'postcode': 'DY1 4BP',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': '1st floor is solid brick with external wall insulation. 2nd floor is cavity, '
                               'retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            'Wall Render': None,
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Side house direction: North East',
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90070461,
            # 'address': '8 Corporation Road',
            # 'postcode': 'DY2 7PX',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': "External wall insulation",
            'Wall Render': "Render finish throughout",
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Front house direction: North East, Back house direction: South West',
            'Access to mains?': None,
        },
        {
            'uprn': 90022227,
            # 'address': '21 Wells Road',
            # 'postcode': 'DY5 3TB',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            'Wall Render': None,
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Front house direction: East, Back house direction: West',
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90077535,
            # 'address': '47 Fairfield Road',
            # 'postcode': 'DY8 5UJ',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            'Wall Render': None,
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Front house direction: East, Back house direction: West',
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90060989,
            # 'address': '53 Bromley',
            # 'postcode': 'DY5 4PJ',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': "Filled at build, partially filled - celotex/king board, 50mm cavity remaining - "
                               "recommends a cavity wall fill",
            "Roof": "Hipped roof",
            'Existing solar PV': 'No existing solar',
            'Orientation': "Front house direction: North, Back house direction: South, Side house direction: West",
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90048026,
            # 'address': '91 Osprey Drive',
            # 'postcode': 'DY1 2JS',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            'Wall Render': 'Tile hung front and rear of property',
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Side house direction: East',
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90093693,
            # 'address': '150 Huntingtree Road',
            # 'postcode': 'B63 4HP',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Heating': 'Electric (storage heaters)',
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            "Roof": "Hipped roof",
            'Existing solar PV': 'No existing solar',
            'Orientation': "Front house direction: North West, Back house direction: South East, Side house direction: "
                           "North East",
        },
        {
            'uprn': 90051858,
            # 'address': '195 Ashenhurst Road',
            # 'postcode': 'DY1 2JB',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            'Wall Render': "Solid render front and rear of property",
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Front house direction: South, Back house direction: North',
            'Access to mains?': 'Property has access to the mains',
        },
        {
            'uprn': 90106884,
            # 'address': '27 Milton Road',
            # 'postcode': 'WV14 8HZ',
            'surveyor': 'Carl Fitzgerald - The Warmfront Team',
            'survey_date': datetime.strptime('2024-04-11', '%Y-%m-%d'),
            'Wall Insulation': 'Cavity wall, retro drilled, containing loose fibre insulation. Consider getting a '
                               'CIGA check and extracting the cavity, replacing with bead insulation.',
            'Wall Render': "Solid render front and rear of property",
            'Existing solar PV': 'No existing solar',
            'Orientation': 'Front house direction: South East, Back house direction: North West',
            'Access to mains?': 'Property has access to the mains',
        },
    ]

    session = sessionmaker(bind=db_engine)()
    upload_non_intrusive_survey_notes(session=session, non_invasive_notes=non_invasive_notes, batch_size=500)
