from pathlib import Path

from rights_filter.server.image_store import LocalSubmissionImageStore


def test_local_image_store_reads_manifest_and_payload(tmp_path: Path):
    image_dir = tmp_path / "images"
    image_dir.mkdir()
    image_file = image_dir / "sample.svg"
    image_file.write_text("<svg><!-- FACE PERSON --></svg>", encoding="utf-8")
    (tmp_path / "submissions.json").write_text(
        """
        [
          {
            "id": "SUB-T1",
            "title": "Local sample",
            "file": "images/sample.svg",
            "width": 640,
            "height": 480,
            "submitted_at": "2026-05-26 10:00"
          }
        ]
        """,
        encoding="utf-8",
    )

    store = LocalSubmissionImageStore(tmp_path)

    records = store.submission_records()
    payload = store.image_payload("SUB-T1")

    assert records[0]["id"] == "SUB-T1"
    assert records[0]["asset"] == "/media/images/sample.svg"
    assert payload.content == image_file.read_bytes()
    assert payload.width == 640
    assert payload.height == 480


def test_local_image_store_includes_unlisted_image_files_when_manifest_exists(tmp_path: Path):
    image_dir = tmp_path / "images"
    image_dir.mkdir()
    (image_dir / "listed.svg").write_text("<svg></svg>", encoding="utf-8")
    (image_dir / "copied.png").write_bytes(b"png-bytes")
    (tmp_path / "submissions.json").write_text(
        """
        [
          {
            "id": "SUB-LISTED",
            "title": "Listed sample",
            "file": "images/listed.svg",
            "width": 640,
            "height": 480
          }
        ]
        """,
        encoding="utf-8",
    )

    records = LocalSubmissionImageStore(tmp_path).submission_records()

    assert [record["id"] for record in records] == ["SUB-LISTED", "copied"]
    assert records[1]["title"] == "copied"
    assert records[1]["asset"] == "/media/images/copied.png"


def test_local_image_store_scans_avif_submissions(tmp_path: Path):
    image_dir = tmp_path / "images"
    image_dir.mkdir()
    (image_dir / "portrait.avif").write_bytes(b"avif-bytes")

    records = LocalSubmissionImageStore(tmp_path).submission_records()
    payload = LocalSubmissionImageStore(tmp_path).image_payload("portrait")

    assert records[0]["id"] == "portrait"
    assert records[0]["asset"] == "/media/images/portrait.avif"
    assert records[0]["format"] == "AVIF"
    assert payload.metadata["format"] == "AVIF"


def test_local_image_store_skips_manifest_records_with_missing_files(tmp_path: Path):
    image_dir = tmp_path / "images"
    image_dir.mkdir()
    (image_dir / "copied.jpg").write_bytes(b"jpg-bytes")
    (tmp_path / "submissions.json").write_text(
        """
        [
          {
            "id": "SUB-MISSING",
            "title": "Missing sample",
            "file": "images/missing.svg",
            "width": 640,
            "height": 480
          }
        ]
        """,
        encoding="utf-8",
    )

    records = LocalSubmissionImageStore(tmp_path).submission_records()

    assert [record["id"] for record in records] == ["copied"]


def test_local_image_store_rejects_manifest_paths_outside_root(tmp_path: Path):
    (tmp_path / "submissions.json").write_text(
        """
        [
          {
            "id": "SUB-BAD",
            "title": "Bad sample",
            "file": "../secret.svg",
            "width": 100,
            "height": 100
          }
        ]
        """,
        encoding="utf-8",
    )

    store = LocalSubmissionImageStore(tmp_path)

    try:
        store.submission_records()
    except ValueError as exc:
        assert "outside image store" in str(exc)
    else:
        raise AssertionError("expected unsafe manifest path to be rejected")
