"""Tests for transcription.ui.jobs_page."""

from uuid import uuid4

import pytest

from transcription.models import Document, Job, Transcript
from transcription.ui.jobs_page import fetch_job_detail, fetch_jobs


@pytest.mark.integration
class TestJobsListBehavior:
    """Verify job list data and rendering helpers."""

    def test_fetch_jobs_returns_job_view_rows(self, session, monkeypatch):
        """fetch_jobs returns normalized JobView rows for UI consumption."""
        document = Document(filename="letter.jpg", file_path="uploads/letter.jpg")
        session.add(document)
        session.commit()
        session.refresh(document)

        job = Job(document_id=document.id)
        session.add(job)
        session.commit()

        class _SessionContext:
            def __enter__(self):
                return session

            def __exit__(self, exc_type, exc, tb):
                return False

        monkeypatch.setattr("transcription.ui.jobs_page.get_session", lambda: _SessionContext())

        rows = fetch_jobs()

        assert len(rows) == 1
        assert rows[0].id == job.id
        assert rows[0].status == "queued"


@pytest.mark.integration
class TestJobDetailBehavior:
    """Verify job detail retrieval behavior."""

    def test_fetch_job_detail_returns_related_records_when_present(self, session, monkeypatch):
        """fetch_job_detail returns job, document, and transcript when available."""
        document = Document(filename="typed.jpg", file_path="uploads/typed.jpg")
        session.add(document)
        session.commit()
        session.refresh(document)

        job = Job(document_id=document.id)
        session.add(job)
        session.commit()
        session.refresh(job)

        transcript = Transcript(job_id=job.id, text="Transcript text")
        session.add(transcript)
        session.commit()

        class _SessionContext:
            def __enter__(self):
                return session

            def __exit__(self, exc_type, exc, tb):
                return False

        monkeypatch.setattr("transcription.ui.jobs_page.get_session", lambda: _SessionContext())

        fetched_job, fetched_document, fetched_transcript = fetch_job_detail(job.id)

        assert fetched_job is not None
        assert fetched_document is not None
        assert fetched_transcript is not None
        assert fetched_job.id == job.id
        assert fetched_document.id == document.id
        assert fetched_transcript.job_id == job.id

    def test_fetch_job_detail_returns_nones_for_missing_job(self, session, monkeypatch):
        """fetch_job_detail returns triple None when job does not exist."""
        class _SessionContext:
            def __enter__(self):
                return session

            def __exit__(self, exc_type, exc, tb):
                return False

        monkeypatch.setattr("transcription.ui.jobs_page.get_session", lambda: _SessionContext())

        fetched_job, fetched_document, fetched_transcript = fetch_job_detail(uuid4())

        assert fetched_job is None
        assert fetched_document is None
        assert fetched_transcript is None
