# -*- coding: utf-8 -*-
"""
test_server.py - End-to-End-Test der Berechnungs-/Grafik-Pipeline auf dem Server.
Prueft: HD-Engine, Radix-Engine und Bodygraph-Rendering mit den Ephemeris-Dateien
im Unterordner ephe/.
"""
import os
import matplotlib
matplotlib.use('Agg')  # headless: kein Display auf dem Server
import swisseph as swe

BASE = os.path.dirname(os.path.abspath(__file__))
EPHE = os.path.join(BASE, 'ephe')
swe.set_ephe_path(EPHE)

from hd_engine import compute_chart, print_chart
from hd_bodygraph import render_bodygraph
from radix_engine import compute_radix, print_radix

print("EPHE-Pfad:", EPHE, "| vorhanden:", os.path.isdir(EPHE))

print("\n##### 1) HD-CHART #####")
hd = compute_chart(1976, 3, 31, 14, 0, name='Dirk Zessin')  # 15:00 MEZ -> 14:00 UT
print_chart(hd)

print("\n##### 2) GRUNDHOROSKOP (Radix) #####")
radix = compute_radix(1976, 3, 31, 15, 0, 53.55, 10.0,
                      tz='Europe/Berlin', hsys=b'K', name='Dirk Zessin')
print_radix(radix)

print("\n##### 3) BODYGRAPH RENDERN #####")
out = render_bodygraph(hd, os.path.join(BASE, 'bodygraph_test_server.png'),
                       subtitle_extra='31.03.1976 \u00b7 15:00 \u00b7 Hamburg')
size = os.path.getsize(out)
print(f"Bodygraph gerendert: {out} ({size} Bytes)")

ok_hd = (hd['type'] == 'Generator' and hd['authority'] == 'Sakral')
ac_z = ['Widder','Stier','Zwillinge','Krebs','Loewe','Jungfrau','Waage','Skorpion',
        'Schuetze','Steinbock','Wassermann','Fische'][int(radix['ac']//30)%12]
ok_radix = (ac_z == 'Jungfrau')
ok_png = size > 10000
print(f"\nChecks -> HD:{ok_hd}  Radix-AC-Jungfrau:{ok_radix}  PNG-ok:{ok_png}")
print(">>> SERVER-PIPELINE OK <<<" if (ok_hd and ok_radix and ok_png) else ">>> FEHLER <<<")
