from typing import Any
import logging


def validate_field(field: str, value: Any) -> Any:
    """Type checks a given config field.

    Compare a field value's type with the expected type for that field.
    Return the value if the type matches, otherwise return the default
    value for that field.

    Args:
        field: The name of the field.
        value: The value stored in that field.

    Returns:
        Either the value of the field or the default value of the
        field, depending on whether the value is of the expected type.
    """
    expected_types = {
        "dashboard_title": str,
        "dashboard_favicon": str,
        "covid_nation": str,
        "covid_local": str,
        "covid_update_interval_seconds": int,
        "news_API_key": str,
        "news_country_code": str,
        "news_update_interval_seconds": int,
        "news_search_terms": str
    }

    default_values = {
        "dashboard_title": "COVID-19 Dashboard",
        "dashboard_favicon": "https://i.ibb.co/7nYCWzN/Favicon.png",
        "covid_nation": "England",
        "covid_local": "Exeter",
        "covid_update_interval_seconds": 86400,
        "news_API_key": "",
        "news_country_code": "gb",
        "news_update_interval_seconds": 86400,
        "news_search_terms": "Covid COVID-19 coronavirus"
    }
    if isinstance(value, expected_types[field]):
        return value

    type_error_log = "Config field '%s' invalid type. %s should be: %s"
    actual = type(value).__name__
    expected = expected_types[field].__name__
    logging.warning(type_error_log, field, actual, expected)

    default_value = default_values[field]
    replace_log = "Invalid config value '%s' replaced with '%s'"
    logging.warning(replace_log, value, default_value)
    return default_value