"""Add more information about the landmarks by writing a short description and keywords. """


def description_and_keywords(tags: dict):
    """
    Generates a description and a set of keywords for a given landmark based on its tags.

    Params:
        tags (dict): A dictionary containing metadata about the landmark, including its name,
                importance, height, date of construction, and visitor information.

    Returns:
        description (str): A string description of the landmark.
        keywords (dict): A dictionary of keywords with fields such as 'importance', 'height',
                    'place_type', and 'date'.
    """
    # Extract relevant fields
    name = tags.get('name')
    importance = tags.get('importance', None)
    n_visitors = tags.get('tourism:visitors', None)
    height = tags.get('height')
    place_type = get_place_type(tags)
    date = get_date(tags)

    if place_type is None :
        return None, None

    # Start the description.
    if importance is None :
        if len(tags.keys()) < 5 :
            return None, None
        if len(tags.keys()) < 10 :
            description = f"{name} is a well known {place_type}."
        elif len(tags.keys()) < 17 :
            importance = 'national'
            description = f"{name} is a {place_type} of national importance."
        else :
            importance = 'international'
            description = f"{name} is an internationally famous {place_type}."
    else :
        description = f"{name} is a {place_type} of {importance} importance."

    if height is not None and date is not None :
        description += f" This {place_type} was constructed in {date} and is ca. {height} meters high."
    elif height is not None :
        description += f" This {place_type} stands ca. {height} meters tall."
    elif date is not None:
        description += f" It was constructed in {date}."

    # Format the visitor number
    if n_visitors is not None :
        n_visitors = int(n_visitors)
        if n_visitors < 1000000 :
            description += f" It welcomes {int(n_visitors/1000)} thousand visitors every year."
        else :
            description += f" It welcomes {round(n_visitors/1000000, 1)} million visitors every year."

    # Set the keywords.
    keywords = {"importance": importance,
                "height": height,
                "place_type": place_type,
                "date": date}

    return description, keywords


def get_place_type(tags):
    """
    Determines the type of the place based on available tags such as 'amenity', 'building',
    'historic', and 'leisure'. The priority order is: 'historic' > 'building' (if not generic) > 
    'amenity' > 'leisure'.

    Params:
        tags (dict): A dictionary containing metadata about the place.

    Returns:
        place_type (str): The determined type of the place, or None if no relevant type is found.
    """
    amenity = tags.get('amenity', None)
    building = tags.get('building', None)
    historic = tags.get('historic', None)
    leisure = tags.get('leisure')

    if historic and historic != "yes":
        return historic
    if building and building not in ["yes", "civic", "government", "apartments", "residential", "commericial", "industrial", "retail", "religious", "public", "service"]:
        return building
    if amenity:
        return amenity
    if leisure:
        return leisure


    return None


def get_date(tags):
    """
    Extracts the most relevant date from the available tags, prioritizing 'construction_date',
    'start_date', 'year_of_construction', and 'opening_date' in that order.

    Params:
        tags (dict): A dictionary containing metadata about the place.

    Returns:
        date (str): The most relevant date found, or None if no date is available.
    """
    construction_date = tags.get('construction_date', None)
    opening_date = tags.get('opening_date', None)
    start_date = tags.get('start_date', None)
    year_of_construction = tags.get('year_of_construction', None)

    # Prioritize based on availability
    if construction_date:
        return construction_date
    if start_date:
        return start_date
    if year_of_construction:
        return year_of_construction
    if opening_date:
        return opening_date

    return None