"""Generate seven illustrative monitoring polygons for five oil chokepoints.

These are intentionally buffered editorial query areas, not official waterway
boundaries or vessel navigation data. Coordinates are longitude, latitude.
"""
import json
from pathlib import Path

OUT = Path(__file__).resolve().parent


def closed_ccw(points):
    ring = [list(p) for p in points]
    if ring[-1] != ring[0]:
        ring.append(ring[0])
    area2 = sum(a[0] * b[1] - b[0] * a[1] for a, b in zip(ring, ring[1:]))
    if area2 < 0:
        ring.reverse()
    return ring


def rectangle(west, south, east, north):
    return [(west, south), (east, south), (east, north), (west, north)]


AREAS = [
    {
        "id": "hormuz", "name": "Strait of Hormuz", "short": "Hormuz",
        "extent": [54.8, 25.1, 58.0, 27.9],
        "scope": "Strait and approaches between Iran and Oman",
        "parts": [
            ("Hormuz", [(55.55, 26.25), (56.0, 25.85), (56.9, 25.65),
                         (57.3, 26.25), (56.65, 27.3), (55.55, 27.15)])
        ],
    },
    {
        "id": "malacca", "name": "Strait of Malacca", "short": "Malacca",
        "extent": [94.0, 0.1, 105.2, 8.5],
        "scope": "Malacca corridor and Singapore approaches",
        "parts": [
            ("Malacca and Singapore approaches", [
                (94.8, 6.0), (98.5, 7.85), (100.45, 6.0), (100.9, 4.15),
                (101.95, 2.8), (103.15, 1.85), (104.5, 1.6), (104.5, 0.85),
                (103.4, 0.95), (102.25, 1.5), (100.6, 2.45), (99.0, 3.65),
                (97.4, 4.85)
            ])
        ],
    },
    {
        "id": "suez", "name": "Suez Canal", "short": "Suez",
        "extent": [31.4, 29.4, 33.2, 31.8],
        "scope": "Port Said, canal corridor, and Suez approaches",
        "parts": [
            ("Suez Canal corridor", [(32.05, 31.45), (32.6, 31.45),
                                      (32.65, 30.7), (32.85, 29.7),
                                      (32.35, 29.7), (32.15, 30.4)])
        ],
    },
    {
        "id": "danish", "name": "Danish Straits", "short": "Danish Straits",
        "extent": [8.6, 54.1, 13.8, 57.0],
        "scope": "Little Belt, Great Belt, and Øresund",
        "parts": [
            ("Little Belt", rectangle(9.45, 54.65, 10.2, 55.85)),
            ("Great Belt", rectangle(10.5, 54.65, 11.5, 56.15)),
            ("Øresund", rectangle(12.25, 55.2, 13.1, 56.25)),
        ],
    },
    {
        "id": "bab", "name": "Bab el-Mandeb", "short": "Bab el-Mandeb",
        "extent": [41.8, 10.9, 44.7, 14.0],
        "scope": "Southern Red Sea gateway and Gulf of Aden approaches",
        "parts": [
            ("Bab el-Mandeb and approaches", [
                (42.55, 13.35), (43.2, 13.4), (44.0, 12.45),
                (43.6, 11.6), (42.8, 11.55), (42.45, 12.5)
            ])
        ],
    },
]


def build():
    features = []
    for area in AREAS:
        for name, points in area["parts"]:
            features.append({
                "type": "Feature",
                "properties": {"chokepoint_id": area["id"], "chokepoint": area["name"],
                               "name": name, "boundary_kind": "illustrative_buffered_monitoring_area"},
                "geometry": {"type": "Polygon", "coordinates": [closed_ccw(points)]},
            })
    geometry = {"type": "MultiPolygon", "coordinates": [f["geometry"]["coordinates"] for f in features]}
    named = {"type": "FeatureCollection", "features": features}
    request = {"polygon": geometry, "from": "2026-09-01", "to": "2026-09-30",
               "order": "desc", "limit": 500, "view": "lite"}
    files = {"oil-chokepoints.geojson": geometry,
             "oil-chokepoints-named.geojson": named,
             "oil-chokepoints-request.json": request}
    for filename, value in files.items():
        (OUT / filename).write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n")
    return named, geometry


if __name__ == "__main__":
    named, geometry = build()
    print(f"Generated {len(AREAS)} chokepoints, {len(named['features'])} polygons.")
    print(f"Request body: {OUT / 'oil-chokepoints-request.json'}")
