#!/usr/bin/env python3
from pathlib import Path
from datetime import datetime

p = Path('/srv/apps/sotlab-users/sotlab_users_app.py')
s = p.read_text()
bak = p.with_name(p.name + '.bak.music-login.' + datetime.now().strftime('%Y%m%d%H%M%S'))
bak.write_text(s)

old = """def render_login(service_label, error='', next_path='/'):
    err = f'<div class="errors">{esc(error)}</div>' if error else ''
    body = f'''<header><div class="hero-logo"><img src="{LOGO_URL}" alt="Sot Lab logo"></div><div class="eyebrow">Sot Lab access</div><h1>Sign in for {esc(service_label)}</h1><p class="lead">Use the username and assigned password from your enabled Sot Lab profile.</p></header><section class="panel">{err}<form method="post" action="/login"><input type="hidden" name="next" value="{esc(next_path)}"><label>User Name</label><input name="username" autocomplete="username" required><label>Password</label><input type="password" name="password" autocomplete="current-password" required><p style="margin-top:18px"><button class="button" type="submit">Sign in</button></p></form></section>'''
    return page(f'Sign in — {service_label}', body)
"""

new = """def render_login(service_label, error='', next_path='/'):
    err = f'<div class="errors">{esc(error)}</div>' if error else ''
    if service_label == 'Sot Lab Music':
        body = f'''<header><div class="hero-logo"><img src="{LOGO_URL}" alt="Sot Lab logo"></div><div class="eyebrow">Private beta · music.sotlab.co.uk</div><h1>Sot Lab Music</h1><p class="lead">A private NAS-backed music room for approved Sot Lab profiles: albums, search, playlists, browser playback, and mobile app support once your profile has Music enabled.</p></header><section class="panel" style="overflow:hidden;position:relative"><div style="position:absolute;right:-80px;top:-110px;width:300px;height:300px;border-radius:50%;background:repeating-radial-gradient(circle,rgba(125,211,252,.16) 0 1px,transparent 1px 16px);opacity:.7"></div><div class="row" style="align-items:center;position:relative"><div><div class="eyebrow">Credentials required</div><h2 style="font-size:clamp(1.8rem,4vw,3.2rem);line-height:.95;letter-spacing:-.06em;margin:10px 0 12px">Sign in to the listening room.</h2><p class="muted">Use your Sot Lab username and assigned password. If your profile does not have <strong>Music</strong> enabled, access stays locked even with valid credentials.</p><div class="checks" style="margin-top:18px"><div class="check">◇ Private access</div><div class="check">◇ Read-only NAS library</div><div class="check">◇ Playlists & search</div><div class="check">◇ Downloads disabled by default</div></div></div><form method="post" action="/login" style="border:1px solid var(--line);background:rgba(255,255,255,.045);border-radius:22px;padding:18px"><input type="hidden" name="next" value="{esc(next_path)}">{err}<label>User Name</label><input name="username" autocomplete="username" required><label>Password</label><input type="password" name="password" autocomplete="current-password" required><p style="margin-top:18px"><button class="button" type="submit">Open Music →</button></p><p class="muted" style="font-size:.92rem">Need access? Ask Sott to enable Music on your Sot Lab profile.</p></form></div></section>'''
        return page('Sign in — Sot Lab Music', body)
    body = f'''<header><div class="hero-logo"><img src="{LOGO_URL}" alt="Sot Lab logo"></div><div class="eyebrow">Sot Lab access</div><h1>Sign in for {esc(service_label)}</h1><p class="lead">Use the username and assigned password from your enabled Sot Lab profile.</p></header><section class="panel">{err}<form method="post" action="/login"><input type="hidden" name="next" value="{esc(next_path)}"><label>User Name</label><input name="username" autocomplete="username" required><label>Password</label><input type="password" name="password" autocomplete="current-password" required><p style="margin-top:18px"><button class="button" type="submit">Sign in</button></p></form></section>'''
    return page(f'Sign in — {service_label}', body)
"""

if old not in s:
    raise SystemExit('render_login block not found')
p.write_text(s.replace(old, new))
print(f'backup={bak}')
