from pathlib import Path

from fastapi.testclient import TestClient

from backend.main import ROOT, app
from scripts.generate_sample import make_frame

import cv2


client = TestClient(app)


def test_health_and_methods():
    health = client.get("/api/health")
    assert health.status_code == 200
    assert health.json()["status"] == "ok"
    methods = client.get("/api/methods")
    assert methods.status_code == 200
    assert "fusion" in methods.json()["methods"]
    samples = client.get("/api/samples")
    assert samples.status_code == 200
    assert "samples" in samples.json()
    if samples.json()["samples"]:
        sample = samples.json()["samples"][0]
        assert "version" in sample
        assert "?v=" in sample["url"]


def test_segment_image(tmp_path: Path):
    image_path = tmp_path / "sample.png"
    cv2.imwrite(str(image_path), make_frame(8, 320, 220))
    with image_path.open("rb") as handle:
        response = client.post(
            "/api/segment",
            files={"file": ("sample.png", handle, "image/png")},
            data={"method": "fusion", "sensitivity": "0.68"},
        )
    assert response.status_code == 200
    payload = response.json()
    assert payload["kind"] == "image"
    assert payload["frames"][0]["metrics"]["mask_pixels"] > 0
    assert payload["frames"][0]["source_time"] == 0.0
    assert payload["frames"][0]["result_time"] == 0.0


def test_segment_video_and_compare_frame(tmp_path: Path):
    video_path = tmp_path / "sample.mp4"
    writer = cv2.VideoWriter(str(video_path), cv2.VideoWriter_fourcc(*"mp4v"), 12.0, (320, 220))
    for index in range(18):
        writer.write(make_frame(index, 320, 220))
    writer.release()

    with video_path.open("rb") as handle:
        response = client.post(
            "/api/segment",
            files={"file": ("sample.mp4", handle, "video/mp4")},
            data={"method": "fusion", "sensitivity": "0.58", "frame_stride": "6", "max_frames": "3"},
        )
    assert response.status_code == 200
    payload = response.json()
    assert payload["kind"] == "video"
    assert payload["video_url"].endswith(".mp4")
    assert payload["source_fps"] > 0
    assert payload["result_fps"] == payload["source_fps"]
    assert payload["result_duration"] == payload["duration"]
    assert len(payload["frames"]) == 3
    first_frame = payload["frames"][0]
    assert first_frame["source_time"] == 0.0
    assert first_frame["result_time"] == 0.0
    assert first_frame["result_index"] == 0
    second_frame = payload["frames"][1]
    assert second_frame["result_time"] == second_frame["source_time"]
    assert second_frame["result_index"] == second_frame["frame_index"]

    result_path = ROOT / payload["video_url"].lstrip("/")
    result_capture = cv2.VideoCapture(str(result_path))
    assert result_capture.isOpened()
    result_frames = int(result_capture.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
    result_fps = float(result_capture.get(cv2.CAP_PROP_FPS) or 0)
    result_capture.set(cv2.CAP_PROP_POS_FRAMES, 1)
    ok, carried_overlay = result_capture.read()
    result_capture.set(cv2.CAP_PROP_POS_FRAMES, second_frame["frame_index"])
    ok_selected, selected_video_frame = result_capture.read()
    result_capture.release()
    assert result_frames == 18
    assert abs((result_frames / result_fps) - payload["duration"]) < 0.25
    assert ok
    yellow_pixels = (
        (carried_overlay[:, :, 1] > 140)
        & (carried_overlay[:, :, 2] > 140)
        & (carried_overlay[:, :, 0] < 150)
    ).sum()
    assert yellow_pixels > 0
    assert ok_selected
    selected_overlay = cv2.imread(str(ROOT / second_frame["overlay_url"].lstrip("/")))
    assert selected_overlay is not None
    selected_delta = cv2.absdiff(selected_video_frame, selected_overlay).mean()
    assert selected_delta < 20.0

    with video_path.open("rb") as handle:
        compare = client.post(
            "/api/compare-frame",
            files={"file": ("sample.mp4", handle, "video/mp4")},
            data={"frame_index": str(payload["frames"][1]["frame_index"]), "sensitivity": "0.58"},
        )
    assert compare.status_code == 200
    compare_payload = compare.json()
    assert compare_payload["kind"] == "compare"
    assert {frame["method"] for frame in compare_payload["frames"]} == {
        "hessian_ridge",
        "edge_morphology",
        "temporal_difference",
        "fusion",
    }
