#!/usr/bin/env python3
import os, json
from flask import Flask, render_template_string, abort
app = Flask(__name__)
REPORTS_DIR = "/opt/struktur/mirofish/backend/uploads/reports"
HTML = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MiroFish Reports</title>
<style>
body{font-family:sans-serif;max-width:900px;margin:40px auto;padding:0 20px;background:#f5f5f5}
h1{color:#333}
.report-card{background:#fff;border-radius:8px;padding:20px;margin:10px 0;box-shadow:0 1px 4px rgba(0,0,0,0.1)}
.report-card h3{margin:0 0 8px 0}
.report-card a{color:#0066cc;text-decoration:none;font-weight:bold}
.meta{color:#666;font-size:0.85em;margin-top:5px}
.back{display:inline-block;margin-bottom:20px;color:#0066cc;text-decoration:none}
pre{background:#f0f0f0;padding:15px;border-radius:6px;overflow-x:auto;white-space:pre-wrap;font-size:0.9em}
</style>
</head>
<body>
{% if reports is defined %}
<h1>MiroFish Reports</h1>
{% for r in reports %}
<div class="report-card">
<h3><a href="/report/{{ r.id }}">{{ r.title }}</a></h3>
<div class="meta">{{ r.created }} | {{ r.id }}</div>
<div class="meta">{{ r.summary }}</div>
</div>
{% endfor %}
{% else %}
<a href="/" class="back">← Alle Reports</a>
<h1>{{ title }}</h1>
<div style="background:#fff;padding:20px;border-radius:8px;box-shadow:0 1px 4px rgba(0,0,0,0.1)">
<pre>{{ content }}</pre>
</div>
{% endif %}
</body>
</html>"""
@app.route("/")
def index():
reports = []
for rid in os.listdir(REPORTS_DIR):
full_path = os.path.join(REPORTS_DIR, rid, "full_report.md")
meta_path = os.path.join(REPORTS_DIR, rid, "meta.json")
if not os.path.exists(full_path):
continue
title, summary, created = rid, "", ""
if os.path.exists(meta_path):
try:
m = json.load(open(meta_path))
title = m.get("title", rid)
req = m.get("simulation_requirement", "")
summary = req[:120] + "..." if len(req) > 120 else req
created = m.get("created_at", "")[:16]
except:
pass
reports.append({"id": rid, "title": title, "summary": summary, "created": created})
reports.sort(key=lambda x: x["created"], reverse=True)
return render_template_string(HTML, reports=reports)
@app.route("/report/<report_id>")
def report(report_id):
path = os.path.join(REPORTS_DIR, report_id, "full_report.md")
if not os.path.exists(path):
abort(404)
content = open(path, encoding="utf-8").read()
return render_template_string(HTML, title=report_id, content=content)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5055)