import zipfile
import json
from io import BytesIO

import cv2
import numpy as np


def _seed_export_data(client):
    project = client.post("/api/projects", json={"name": "Export Project"}).json()
    frame = client.post(f"/api/projects/{project['id']}/frames", json={
        "project_id": project["id"],
        "frame_index": 0,
        "image_url": "frames/0.jpg",
        "width": 100,
        "height": 50,
    }).json()
    template = client.post("/api/templates", json={
        "name": "Category",
        "color": "#06b6d4",
        "z_index": 0,
        "classes": [],
        "rules": [],
    }).json()
    annotation = client.post("/api/ai/annotate", json={
        "project_id": project["id"],
        "frame_id": frame["id"],
        "template_id": template["id"],
        "mask_data": {"polygons": [[[0.1, 0.2], [0.9, 0.2], [0.9, 0.8], [0.1, 0.8]]]},
        "points": [[0.5, 0.5]],
        "bbox": [0.1, 0.2, 0.8, 0.6],
    }).json()
    return project, frame, template, annotation


def test_export_coco_json_structure(client):
    project, frame, _, _ = _seed_export_data(client)

    response = client.get(f"/api/export/{project['id']}/coco")

    assert response.status_code == 200
    assert response.headers["content-type"].startswith("application/json")
    data = response.json()
    assert data["info"]["description"] == "Annotations for Export Project"
    assert data["images"][0] == {
        "id": frame["id"],
        "file_name": "frames/0.jpg",
        "width": 100,
        "height": 50,
        "frame_index": 0,
    }
    assert data["annotations"][0]["segmentation"] == [[10.0, 10.0, 90.0, 10.0, 90.0, 40.0, 10.0, 40.0]]
    assert data["annotations"][0]["bbox"] == [10.0, 10.0, 80.0, 30.000000000000004]
    assert data["categories"][0]["name"] == "Category"


def test_export_masks_zip(client):
    project, _, _, annotation = _seed_export_data(client)

    response = client.get(f"/api/export/{project['id']}/masks")

    assert response.status_code == 200
    assert response.headers["content-type"].startswith("application/zip")
    with zipfile.ZipFile(BytesIO(response.content)) as archive:
        assert archive.namelist() == [
            f"mask_{annotation['id']:06d}.png",
            "semantic_frame_000000.png",
            "semantic_classes.json",
        ]


def test_export_masks_uses_z_index_for_semantic_fusion(client):
    project = client.post("/api/projects", json={"name": "Fusion Project"}).json()
    frame = client.post(f"/api/projects/{project['id']}/frames", json={
        "project_id": project["id"],
        "frame_index": 0,
        "image_url": "frames/0.jpg",
        "width": 20,
        "height": 20,
    }).json()
    low = client.post("/api/ai/annotate", json={
        "project_id": project["id"],
        "frame_id": frame["id"],
        "mask_data": {
            "polygons": [[[0.1, 0.1], [0.8, 0.1], [0.8, 0.8], [0.1, 0.8]]],
            "label": "Low",
            "color": "#00ff00",
            "class": {"id": "low", "name": "Low", "color": "#00ff00", "zIndex": 10},
        },
    }).json()
    high = client.post("/api/ai/annotate", json={
        "project_id": project["id"],
        "frame_id": frame["id"],
        "mask_data": {
            "polygons": [[[0.4, 0.4], [0.9, 0.4], [0.9, 0.9], [0.4, 0.9]]],
            "label": "High",
            "color": "#ff0000",
            "class": {"id": "high", "name": "High", "color": "#ff0000", "zIndex": 20},
        },
    }).json()

    response = client.get(f"/api/export/{project['id']}/masks")

    assert response.status_code == 200
    with zipfile.ZipFile(BytesIO(response.content)) as archive:
        assert f"mask_{low['id']:06d}.png" in archive.namelist()
        assert f"mask_{high['id']:06d}.png" in archive.namelist()
        legend = json.loads(archive.read("semantic_classes.json"))
        high_value = next(item["value"] for item in legend["classes"] if item["key"] == "class:high")
        semantic_bytes = np.frombuffer(archive.read("semantic_frame_000000.png"), dtype=np.uint8)
        semantic = cv2.imdecode(semantic_bytes, cv2.IMREAD_GRAYSCALE)

    assert semantic[10, 10] == high_value


def test_export_missing_project_returns_404(client):
    assert client.get("/api/export/999/coco").status_code == 404
    assert client.get("/api/export/999/masks").status_code == 404
