from typing import Tuple
from typing import Optional
from datetime import datetime


def parse_timestamp(line: str) -> Tuple[Optional[datetime], str]:
    """
    Extracts the leading timestamp of a chat line
    :param line: Line to parse
    :return: a tuple with the timestamp (if any) and the rest of the line.
    """
    datetime_formats = [
        '%d/%m/%y %H:%M ',
        '%d/%m/%y, %H:%M ',
        '%d/%m/%Y, %H:%M ',
        '%Y-%m-%d, %H:%M ',
    ]

    for datetime_format in datetime_formats:
        try:
            segments = line.split('-')
            separator_pos = datetime_format.count('-') + 1
            date_time = '-'.join(segments[0:separator_pos])
            rest_of_line = '-'.join(segments[separator_pos:])
            t = datetime.strptime(date_time, datetime_format)
            return t, rest_of_line
        except ValueError:
            pass

    try:
        fields = line.split('] ', 1)
        if (len(fields) != 2):
            return None, line
        date_time, rest_of_line = fields
        datetime_format = '[%d/%m/%y, %H:%M:%S'
        t = datetime.strptime(date_time, datetime_format)
        return t, rest_of_line
    except ValueError:
        pass

    return None, line