#!/usr/bin/env python3
"""Regenerate the Home 'Today's Tasks' notebook crop:
- TALLER crop (more of the journal so the paper is taller)
- Soft alpha fade at the TOP so the sprig gradually fades out (no hard cut)
- Torn bottom edge (minimal, no shadow)
"""
from PIL import Image, ImageDraw
import random, os

SRC = 'assets/pixiesprout/notebook/journal-fullpage.png'
OUT = 'assets/pixiesprout/notebook/journal-top-torn.png'

im = Image.open(SRC).convert('RGBA')
W, H = im.size

# Taller crop: top ~55% of the full journal (was 45%). More paper visible.
CROP_FRAC = 0.55
crop_h = int(H * CROP_FRAC)
top = im.crop((0, 0, W, crop_h))

# ---- Soft alpha fade at the TOP so the sprig gradually fades out ----
# Fade the top ~12% of the crop from transparent (row 0) to fully opaque.
FADE_FRAC = 0.12
fade_h = int(crop_h * FADE_FRAC)
alpha = top.split()[3]  # alpha channel
fade = Image.new('L', (W, crop_h), 255)
fd = ImageDraw.Draw(fade)
for y in range(fade_h):
    a = int(255 * (y / fade_h))  # 0 at very top -> 255 at fade end
    fd.line([(0, y), (W, y)], fill=a)
# combine: new_alpha = min(orig_alpha, fade) so we only ever reduce
import numpy as np
na = np.minimum(np.array(alpha), np.array(fade)).astype('uint8')
top.putalpha(Image.fromarray(na, 'L'))

# ---- Torn bottom edge (minimal, no shadow) ----
random.seed(7)
vb = top.copy()
mask = Image.new('L', (W, crop_h), 255)
md = ImageDraw.Draw(mask)
tear_y = int(crop_h * 0.80)  # place tear at 80% of the taller crop
pts = []
x = 0
while x <= W:
    wob = random.randint(-14, 14)
    pts.append((x, tear_y + wob))
    x += random.randint(16, 30)
pts.append((W, tear_y + random.randint(-14, 14)))
pts.append((W, crop_h))
pts.append((0, crop_h))
md.polygon(pts, fill=0)
vb = Image.composite(vb, Image.new('RGBA', (W, crop_h), (0, 0, 0, 0)), mask)

vb.save(OUT)
print('Saved', OUT, vb.size)
