from datetime import datetime


def parse_date_string(value: str):
    if not isinstance(value, str):
        return value

    # Remove extra whitespace
    value = value.strip()

    formats = [
        # DateTimeTZ formats (more specific first)
        "%Y-%m-%d %H:%M:%S%z",  # 2026-01-04 14:30:00+0000
        "%Y-%m-%d %H:%M:%S.%f%z",  # With microseconds
        "%Y-%m-%dT%H:%M:%S%z",  # ISO T separator
        "%Y-%m-%dT%H:%M:%S.%f%z",
        # Datetime formats (more specific first)
        "%Y-%m-%d %H:%M:%S.%f",
        "%Y-%m-%d %H:%M:%S",
        "%Y-%m-%dT%H:%M:%S.%f",
        "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%dT%H:%M:%SZ",  # Standard for JS/JSON
        # Date only formats
        "%Y-%m-%d",
        "%d/%m/%Y",
        "%m/%d/%Y",
        "%Y/%m/%d",
        "%d-%b-%Y",  # e.g., 03-Jan-2026
    ]

    for fmt in formats:
        try:
            dt = datetime.strptime(value, fmt)
            # If the format didn't include time components, return a date object
            if fmt in ["%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y", "%Y/%m/%d", "%d-%b-%Y"]:
                return dt.date()
            return dt
        except ValueError:
            continue

    return None
