from flask import Flask, render_template, send_file
from gen import generate_map
from datetime import datetime
import zipfile
import os
import io
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)

if __name__ == "__main__":
    app.run(debug=True)
    
xml_path = os.path.join(BASE_DIR, "data", "IDQ11295.xml")
last_updated = datetime.fromtimestamp(os.path.getmtime(xml_path)).strftime("%A %d %B %Y, %H:%M")

@app.route("/download-all")
def download_all_images():
    buffer = io.BytesIO()
    with zipfile.ZipFile(buffer, "w") as z:
        static_dir = os.path.join(BASE_DIR, "static")
        for filename in os.listdir(static_dir):
            if filename.endswith(".png") and filename.startswith("out"):
                z.write(os.path.join(static_dir, filename), arcname=filename)

    buffer.seek(0)

    # Create a timestamp like "2025-07-06_22-45"
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
    zip_name = f"weather_maps_{timestamp}.zip"

    return send_file(
        buffer,
        mimetype="application/zip",
        as_attachment=True,
        download_name=zip_name
    )

@app.route("/")
def index():
    maps = []

    regions = [
        ("qState",       "baseQ_stateToday.png",       "outQ_stateToday.png",       "today",    "qState",     "0"),
        ("qFarNorth",    "baseQ_farNorthToday.png",    "outQ_farNorthToday.png",    "today",    "qFarNorth",  "0"),
        ("qNorth",       "baseQ_northToday.png",       "outQ_northToday.png",       "today",    "qNorth",     "0"),
        ("qCentral",     "baseQ_centralToday.png",     "outQ_centralToday.png",     "today",    "qCentral",   "0"),
        ("qWideBay",     "baseQ_wideBayToday.png",     "outQ_wideBayToday.png",     "today",    "qWideBay",   "0"),
        ("qSunshine",    "baseQ_sunshineToday.png",    "outQ_sunshineToday.png",    "today",    "qSunshine",  "0"),

        ("qStateTT",     "baseQ_stateTomorrow.png",    "outQ_stateTomorrow.png",    "tomorrow", "qState",     "1"),
        ("qFarNorthTT",  "baseQ_farNorthTomorrow.png", "outQ_farNorthTomorrow.png", "tomorrow", "qFarNorth",  "1"),
        ("qNorthTT",     "baseQ_northTomorrow.png",    "outQ_northTomorrow.png",    "tomorrow", "qNorth",     "1"),
        ("qCentralTT",   "baseQ_centralTomorrow.png",  "outQ_centralTomorrow.png",  "tomorrow", "qCentral",   "1"),
        ("qWideBayTT",   "baseQ_wideBayTomorrow.png",  "outQ_wideBayTomorrow.png",  "tomorrow", "qWideBay",   "1"),
        ("qSunshineTT",  "baseQ_sunshineTomorrow.png", "outQ_sunshineTomorrow.png", "tomorrow", "qSunshine",  "1"),
    ]

    for map_id, base_file, out_file, day, region_id, forecast in regions:
        xml_path = os.path.join(BASE_DIR, "data", "IDQ11295.xml")
        base_path = os.path.join(BASE_DIR, "static", base_file)
        out_path = os.path.join(BASE_DIR, "static", out_file)

        generate_map(
            xml_path=xml_path,
            base_image_path=base_path,
            map_id=region_id,
            forecast_index=forecast,
            output_path=out_path
        )

        maps.append({
            "id": map_id,
            "day": day,
            "image": out_file  # just filename, for /static/{{ map.image }}
        })
        
    return render_template("index.html", maps=maps, last_updated=last_updated)
        
@app.route("/map")
def map():
    return send_file("static/outImg.png", mimetype="image/png")
