#!/usr/bin/env python3
"""
Daily Curiosity #3 — Чому листя восени жовтіє і червоніє?
Schematic: a leaf changes through three states (summer → early autumn → late autumn).
Chlorophyll breaks down, revealing carotenoids (yellow/orange) that were always there,
while anthocyanins (red/purple) are produced fresh.
"""
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch, Circle, FancyBboxPatch, Polygon, Wedge
import numpy as np

# ---- Canvas ----
fig, ax = plt.subplots(figsize=(12, 7), dpi=160)
ax.set_xlim(0, 12)
ax.set_ylim(0, 7)
ax.set_aspect('equal')
ax.axis('off')

# Warm dark autumn gradient background (deep maroon → warm brown)
bg = np.linspace(0, 1, 256).reshape(-1, 1)
bg = np.repeat(bg, 256, axis=1)
ax.imshow(
    bg,
    extent=(0, 12, 0, 7),
    cmap=plt.cm.colors.LinearSegmentedColormap.from_list(
        "autumn_dark", ["#1a0a05", "#2e150a", "#3d1e0a"]
    ),
    aspect='auto',
    zorder=0,
)

# ---- Title and subtitle (Ukrainian) ----
ax.text(
    6, 6.55,
    "Чому листя восени жовтіє і червоніє?",
    ha='center', va='center',
    fontsize=24, fontweight='bold', color='#fff4dc',
    zorder=10,
)
ax.text(
    6, 6.05,
    "Зелений хлорофіл руйнується — і виходять на світло жовті й червоні пігменти",
    ha='center', va='center',
    fontsize=12, style='italic', color='#ffd9a0',
    zorder=10,
)

# ---- Helper: draw a stylized leaf at (cx, cy) with a given color ----
def draw_leaf(cx, cy, scale, fill_color, edge_color, vein_color, zorder=5):
    # Leaf shape — pointy oval using a custom polygon
    n = 80
    t = np.linspace(0, 2 * np.pi, n)
    # Lobed/pointed-leaf-ish silhouette
    r = 1.0 + 0.18 * np.cos(3 * t) - 0.12 * np.sin(2 * t)
    # Stretch vertically so it looks like a leaf
    x = cx + scale * r * np.cos(t) * 0.78
    y = cy + scale * r * np.sin(t) * 1.08
    leaf = Polygon(
        np.column_stack([x, y]),
        closed=True, facecolor=fill_color, edgecolor=edge_color,
        lw=1.6, zorder=zorder,
    )
    ax.add_patch(leaf)
    # Central vein
    ax.plot(
        [cx, cx], [cy - scale * 1.05, cy + scale * 1.0],
        color=vein_color, lw=1.4, zorder=zorder + 1,
    )
    # Side veins
    for off in [-0.55, -0.25, 0.05, 0.35, 0.65]:
        sign_y = cy + off * scale
        side = scale * (0.55 - 0.55 * abs(off - 0.05))
        ax.plot(
            [cx, cx - side], [sign_y, sign_y + side * 0.55],
            color=vein_color, lw=0.9, zorder=zorder + 1,
        )
        ax.plot(
            [cx, cx + side], [sign_y, sign_y + side * 0.55],
            color=vein_color, lw=0.9, zorder=zorder + 1,
        )
    # Stem
    ax.plot(
        [cx, cx], [cy - scale * 1.05, cy - scale * 1.45],
        color=edge_color, lw=2.0, zorder=zorder,
    )

# ---- Three leaves (left → right): summer / early autumn / late autumn ----
# Summer: lush green
draw_leaf(2.3, 3.4, 0.85, "#3aa14a", "#1f5a26", "#1a4720")
ax.text(2.3, 1.55, "Літо",
        ha='center', va='center', fontsize=13, fontweight='bold',
        color='#aef0a8', zorder=10)
ax.text(2.3, 1.18, "хлорофіл — у силі",
        ha='center', va='center', fontsize=10.5, style='italic',
        color='#cfe8b6', zorder=10)

# Early autumn: yellow/orange
draw_leaf(6.0, 3.4, 0.85, "#f0b526", "#7a5a0b", "#5c4408")
ax.text(6.0, 1.55, "Рання осінь",
        ha='center', va='center', fontsize=13, fontweight='bold',
        color='#ffe7a8', zorder=10)
ax.text(6.0, 1.18, "хлорофіл руйнується — видно каротиноїди",
        ha='center', va='center', fontsize=10.5, style='italic',
        color='#ffd9a0', zorder=10)

# Late autumn: red
draw_leaf(9.7, 3.4, 0.85, "#c43027", "#5e120c", "#460a07")
ax.text(9.7, 1.55, "Пізня осінь",
        ha='center', va='center', fontsize=13, fontweight='bold',
        color='#ffb8af', zorder=10)
ax.text(9.7, 1.18, "утворюються нові червоні антоціани",
        ha='center', va='center', fontsize=10.5, style='italic',
        color='#ffc8bd', zorder=10)

# ---- Time-arrow under the leaves ----
time_arrow = FancyArrowPatch(
    (1.0, 0.55), (11.0, 0.55),
    arrowstyle='-|>', mutation_scale=22,
    color='#ffd9a0', lw=2.4, zorder=6,
)
ax.add_patch(time_arrow)
ax.text(6.0, 0.25, "коротший день  •  холодніші ночі",
        ha='center', va='center', fontsize=10.5, color='#ffd9a0', zorder=10)

# ---- Arrows between leaves ----
for x_from, x_to in [(3.25, 5.05), (6.95, 8.75)]:
    a = FancyArrowPatch(
        (x_from, 3.4), (x_to, 3.4),
        arrowstyle='-|>', mutation_scale=18,
        color='#fff4dc', lw=2.0, zorder=6,
    )
    ax.add_patch(a)

# ---- Pigment legend (top-left small panel) ----
legend_box = FancyBboxPatch(
    (0.35, 4.5), 3.4, 1.25,
    boxstyle="round,pad=0.08,rounding_size=0.18",
    fc="#2a1208", ec="#ffd9a0", lw=1.2, zorder=7,
)
ax.add_patch(legend_box)
ax.text(2.05, 5.55, "Пігменти у листку",
        ha='center', va='center', fontsize=11, fontweight='bold',
        color='#fff4dc', zorder=10)
# Three swatches
swatches = [
    ("#3aa14a", "хлорофіл — зелений"),
    ("#f0b526", "каротиноїди — жовто-помаранчеві"),
    ("#c43027", "антоціани — червоно-фіолетові"),
]
for i, (col, label) in enumerate(swatches):
    y = 5.3 - i * 0.25
    sw = FancyBboxPatch(
        (0.55, y - 0.08), 0.25, 0.16,
        boxstyle="round,pad=0.01,rounding_size=0.04",
        fc=col, ec="white", lw=0.6, zorder=10,
    )
    ax.add_patch(sw)
    ax.text(0.92, y, label, ha='left', va='center',
            fontsize=9.5, color='#fff4dc', zorder=10)

# ---- Sun + thermometer-style hint (top-right) ----
# Small sun with downward arrow indicating fewer hours
sun = Circle((10.6, 5.45), 0.32, color='#ffd86b', zorder=8, ec='#ffb73a', lw=1.3)
ax.add_patch(sun)
for ang in range(0, 360, 45):
    a = np.deg2rad(ang)
    x0 = 10.6 + 0.4 * np.cos(a)
    y0 = 5.45 + 0.4 * np.sin(a)
    x1 = 10.6 + 0.6 * np.cos(a)
    y1 = 5.45 + 0.6 * np.sin(a)
    ax.plot([x0, x1], [y0, y1], color='#ffb73a', lw=1.2, zorder=8)
arr_down = FancyArrowPatch(
    (10.6, 5.0), (10.6, 4.55),
    arrowstyle='-|>', mutation_scale=14,
    color='#ffd9a0', lw=1.8, zorder=8,
)
ax.add_patch(arr_down)
ax.text(11.45, 5.45, "менше\nсвітла", ha='left', va='center',
        fontsize=9.5, color='#ffd9a0', zorder=10)
ax.text(11.45, 4.65, "холодніше", ha='left', va='center',
        fontsize=9.5, color='#ffd9a0', zorder=10)

# ---- Bottom note ----
ax.text(
    6.0, 6.85,
    "",  # spacer
    ha='center', va='center', fontsize=1, color='#000', zorder=1,
)

plt.tight_layout()
out = "/sessions/vigilant-cool-goldberg/mnt/daily-curiosity/daily-curiosity-03.png"
plt.savefig(out, dpi=160, facecolor='#1a0a05', bbox_inches='tight', pad_inches=0.15)
print(f"Saved: {out}")
