"""
Radian checker: detects 42 degrees expressed in radians.

42° = 7π/30 radians
Recognizes: 7π/30, 7pi/30, 7*pi/30, 42°, 42 degrees, etc.
"""

import re
import math


def _normalize(s):
    """Normalize whitespace and common separators."""
    s = s.strip()
    # Collapse whitespace
    s = re.sub(r"\s+", "", s)
    return s


def _check_degree_symbol(s):
    """Check for 42° or 42 degrees."""
    cleaned = _normalize(s)
    # Match: 42° or 42deg or 42degrees
    m = re.match(r"^(\d+(?:\.\d+)?)\s*(?:°|degrees?|deg)$", cleaned, re.IGNORECASE)
    if m:
        try:
            val = float(m.group(1))
            if abs(val - 42) < 1e-9:
                return f"42 degrees (= 7π/30 radians)"
        except ValueError:
            pass
    return None


def _check_radian_fraction(s):
    """Check for 7π/30 and variants."""
    cleaned = _normalize(s).lower()

    # Replace unicode π with 'pi'
    cleaned = cleaned.replace("π", "pi")

    # Patterns for 7pi/30:
    # 7pi/30, 7*pi/30, 7·pi/30, (7/30)pi, (7/30)*pi
    patterns = [
        # 7pi/30 or 7*pi/30
        r"^(\d+)\*?pi/(\d+)$",
        # 7/30*pi or 7/30pi
        r"^(\d+)/(\d+)\*?pi$",
    ]

    for pattern in patterns:
        m = re.match(pattern, cleaned)
        if m:
            try:
                num = int(m.group(1))
                den = int(m.group(2))
                if den == 0:
                    continue
                # 7π/30 = 42° = 42 * π/180
                # So check if num/den = 42/180 = 7/30
                if abs(num / den - 7 / 30) < 1e-9:
                    return f"radian: {s} = 7π/30 = 42°"
            except (ValueError, ZeroDivisionError):
                pass

    # Check for (7/30)pi or (7/30)*pi
    m = re.match(r"^\((\d+)/(\d+)\)\*?pi$", cleaned)
    if m:
        try:
            num = int(m.group(1))
            den = int(m.group(2))
            if den != 0 and abs(num / den - 7 / 30) < 1e-9:
                return f"radian: {s} = 7π/30 = 42°"
        except (ValueError, ZeroDivisionError):
            pass

    # Direct radian value: 0.733038... (42 * pi / 180)
    try:
        val = float(cleaned.replace("rad", "").replace("radian", "").replace("radians", ""))
        target = 42 * math.pi / 180  # ≈ 0.7330382858...
        if abs(val - target) < 1e-6:
            return f"radian value: {s} ≈ 7π/30 = 42°"
    except ValueError:
        pass

    return None


def check(value):
    """Check if value represents 42 in radian/degree form."""
    if not isinstance(value, str):
        return None

    s = value.strip()
    if not s:
        return None

    for checker in [_check_degree_symbol, _check_radian_fraction]:
        result = checker(s)
        if result:
            return result

    return None
