"""Tests for transcription.ui.upload_page."""

from pathlib import Path
from uuid import uuid4

import pytest

from transcription.services.upload import UploadError, UploadJobResult
from transcription.ui import upload_page


@pytest.mark.unit
class TestUploadPageBehavior:
    """Verify upload page helper and submission behavior."""

    def test_accepted_upload_types_contains_supported_extensions(self):
        """accepted_upload_types includes all MVP-supported upload extensions."""
        accepted = upload_page.accepted_upload_types()
        assert ".jpg" in accepted
        assert ".jpeg" in accepted
        assert ".png" in accepted
        assert ".tif" in accepted
        assert ".tiff" in accepted
        assert ".pdf" in accepted

    def test_submit_upload_calls_upload_service(self, monkeypatch):
        """submit_upload delegates file persistence and job creation to upload service."""
        expected = UploadJobResult(
            document_id=uuid4(),
            job_id=uuid4(),
            stored_path=Path("uploads/mock.jpg"),
            original_filename="mock.jpg",
        )

        def fake_create_upload_job(*, filename: str, file_bytes: bytes):
            assert filename == "mock.jpg"
            assert file_bytes == b"bytes"
            return expected

        monkeypatch.setattr(upload_page, "create_upload_job", fake_create_upload_job)

        result = upload_page.submit_upload(filename="mock.jpg", file_bytes=b"bytes")
        assert result == expected

    def test_submit_upload_surfaces_upload_error(self, monkeypatch):
        """submit_upload raises UploadError for invalid upload payloads."""
        def fake_create_upload_job(*, filename: str, file_bytes: bytes):
            raise UploadError("invalid payload")

        monkeypatch.setattr(upload_page, "create_upload_job", fake_create_upload_job)

        with pytest.raises(UploadError):
            upload_page.submit_upload(filename="bad.jpg", file_bytes=b"")
