from engine import *
import json, datetime as dt

EVENTS = [
 # (date, label, kind)
 (dt.date(1848,9,1),  'Father falls ill (Sept 1848)', 'father'),
 (dt.date(1849,7,30), 'Death of the father, 30 Jul 1849', 'father'),
 (dt.date(1850,1,25), 'Death of brother Joseph, end Jan 1850', 'sibling'),
 (dt.date(1858,10,5), 'Enters Schulpforta, 5 Oct 1858', 'education'),
 (dt.date(1865,10,15),'Leipzig; finds Schopenhauer, Oct/Nov 1865', 'education'),
 (dt.date(1868,3,15), 'Riding accident (chest), Mar 1868', 'injury'),
 (dt.date(1868,11,8), 'Meets Wagner, 8 Nov 1868', 'friend'),
 (dt.date(1869,2,13), 'Basel chair confirmed, Feb 1869', 'career'),
 (dt.date(1869,4,19), 'Arrives in Basel, 19 Apr 1869', 'career'),
 (dt.date(1870,9,7),  'Dysentery + diphtheria at the front, Sept 1870', 'illness'),
 (dt.date(1872,1,2),  'Birth of Tragedy published, 2 Jan 1872', 'book'),
 (dt.date(1876,4,11), 'Proposal to Mathilde Trampedach, 11 Apr 1876', 'love'),
 (dt.date(1876,8,13), 'Bayreuth festival; break with Wagner, Aug 1876', 'friend'),
 (dt.date(1879,6,14), 'Resignation from Basel accepted, 14 Jun 1879', 'career'),
 (dt.date(1881,8,6),  'Eternal Recurrence at Surlei, early Aug 1881', 'vision'),
 (dt.date(1882,5,13), 'Proposal to Lou Salomé (Lucerne), 13 May 1882', 'love'),
 (dt.date(1883,2,13), 'Zarathustra I finished; Wagner dies, 13 Feb 1883', 'book'),
 (dt.date(1888,9,30), 'Antichrist completed; Twilight preface, 30 Sep 1888', 'book'),
 (dt.date(1888,10,15),'Ecce Homo begun, 44th birthday, 15 Oct 1888', 'book'),
 (dt.date(1889,1,3),  'Collapse in Turin, 3 Jan 1889', 'collapse'),
 (dt.date(1889,1,18), 'Jena asylum, 18 Jan 1889', 'collapse'),
 (dt.date(1897,4,20), "Mother's death, 20 Apr 1897", 'mother'),
 (dt.date(1900,8,25), 'Death at Weimar, 25 Aug 1900', 'death'),
]

def date_jd(d): return swe.julday(d.year,d.month,d.day,12.0,swe.GREG_CAL)

def dasa_at(jd_birth, moon_lon, d):
    per = dasa_periods(jd_birth, moon_lon, levels=3)
    j = date_jd(d)
    m = running(per, j, 1); a = running(per, j, 2); p = running(per, j, 3)
    return m, a, p

CANDS = [('09:30',9,30,0),('09:46',9,46,0),('10:00',10,0,0),('10:07',10,7,0)]

out = {}
for lab,hh,mm,ss in CANDS:
    j = jd_lmt(1844,10,15,hh,mm,ss)
    c = chart(j)
    mo = c['Ca']['lon']
    lord,bal = dasa_balance(mo)
    per = dasa_periods(j, mo, levels=3)
    maha = [(jd_to_date(s).isoformat(), jd_to_date(e).isoformat(), ch[0]) for s,e,ch in per if len(ch)==1]
    ev=[]
    for d,label,kind in EVENTS:
        m,a,p = dasa_at(j, mo, d)
        ev.append(dict(date=d.isoformat(), label=label, kind=kind, maha=m[0][0], antara=a[0][1], prat=p[0][2],
                       a_start=jd_to_date(a[1]).isoformat(), a_end=jd_to_date(a[2]).isoformat(),
                       p_start=jd_to_date(p[1]).isoformat(), p_end=jd_to_date(p[2]).isoformat()))
    out[lab] = dict(lagna=c['La']['lon'], moon=mo, balance=bal, maha=maha, events=ev)
    print('=====', lab, fmt(c['La']['lon']), 'Moon', fmt(mo), 'Budha balance', round(bal,3))
    for s,e,l in maha: print('  ', l, s, '→', e)
    for e in ev:
        print(f"  {e['date']}  {e['maha']}-{e['antara']}-{e['prat']}  [antara {e['a_start']}→{e['a_end']}; prat {e['p_start']}→{e['p_end']}]  {e['label']}")
json.dump(out, open('dasas.json','w'), ensure_ascii=False, indent=1)

# Varga charts at 10:00 (planets) — these barely move in the window
j = jd_lmt(1844,10,15,10,0,0); c = chart(j)
print('\nVARGAS at 10:00')
for n in [9,10,12,3,7,30,60]:
    print(f'D-{n}:', {k: SIGNS[varga(c[k]['lon'],n)] for k in ['La','Sū','Ca','Ma','Bu','Gu','Śu','Śa','Rā','Ke']})
