from typing import List
from datetime import datetime


def parse_task_results(raw_pa11y_results: dict) -> List[dict]:
    """Parse raw Pa11y task data with last test results.

    Example Pa11y task data:
    [
      ...,
      {
       'id': '5d00f3db06e38314002017b5',
       'name': 'SERVICE NAME - PAGE NAME',
       'url': 'https://example.domain/some/endpoint/',
       'timeout': 30000,
       'wait': 0,
       'standard': 'WCAG2AA',
       'ignore': [],
       'actions': [],
       'annotations': [],
       'last_result': {
         'id': '5ec1d70c75e6e50017924065',
         'task': '5d00f3db06e38314002017b5',
         'date': '2020-05-18T00:30:04.190Z',
         'count': {'total': 60, 'error': 3, 'warning': 5, 'notice': 52},
         'ignore': []
        }
       }
     ]

    Expected task name format:
        Domestic - Feedback
        Domestic - Home
        Domestic - Market

    It will skip tasks:
     * without a `-` which separates service name from page name
     * last_result field
     * with last_result other than from today

    Returns a list of task test results.
    Each items represents results for one task (page) tested by Pa11y.
    Items don't contain page name as it's not relevant because we're after aggregate values.

    Example result:
    [
     ...
     {'service': 'Profile', 'errors': 3, 'warnings': 5},
     {'service': 'Profile', 'errors': 3, 'warnings': 4},
     {'service': 'SOO', 'errors': 11, 'warnings': 13},
     {'service': 'SOO', 'errors': 4, 'warnings': 5},
     {'service': 'SSO', 'errors': 3, 'warnings': 4},
     {'service': 'SSO', 'errors': 3, 'warnings': 4}
    ]
    """
    results = []
    date_format = "%Y-%m-%dT%H:%M:%S.%fZ"
    for task in raw_pa11y_results:
        if "-" not in task["name"]:
            continue
        if "last_result" not in task:
            continue
        datetime_object = datetime.strptime(task["last_result"]["date"], date_format)
        if datetime_object.date() != datetime.today().date():
            continue
        service = task["name"].split("-")[0].strip()
        errors = task["last_result"]["count"]["error"]
        warnings = task["last_result"]["count"]["warning"]
        results.append({"service": service, "errors": errors, "warnings": warnings})
    return results