"""Tests for the documents page routes and action handlers."""

import pytest
import pytest_asyncio
from sqlmodel import select

from transcription.db import session_scope
from transcription.db.models import Document, DocumentPerson, DocumentPersonRole, Job, Person, Source


# --- Helper Fixtures ---


@pytest_asyncio.fixture
async def seed_person_and_document():
    """Seed a Person and Document linked by DocumentPerson role."""
    async with session_scope() as session:
        person = Person(full_name="Zenna Cochran")
        session.add(person)
        await session.flush()

        doc = Document(
            name="Letter from Hig",
            document_type="letter",
            archive_identifier="ZC-1924-001",
        )
        session.add(doc)
        await session.flush()

        link = DocumentPerson(
            document_id=doc.id,
            person_id=person.id,
            role=DocumentPersonRole.AUTHOR,
        )
        session.add(link)
        await session.commit()
        return str(doc.id), str(person.id)


# --- Integration Tests for Documents Route Handlers ---


@pytest.mark.integration
class TestDocumentsPageRendering:
    """Verify document list, detail, edit, and deletion route behaviors."""

    def test_documents_page_renders_empty_state(self, app_client):
        _, client = app_client

        response = client.get("/ui/documents")

        assert response.status_code == 200
        assert "Archival Documents" in response.text
        assert "No documents in repository yet." in response.text

    @pytest.mark.asyncio
    async def test_documents_page_lists_seeded_documents(self, app_client):
        _, client = app_client

        async with session_scope() as session:
            doc = Document(name="1924 Postcard", document_type="postcard", archive_identifier="PC-001")
            session.add(doc)
            await session.commit()

        response = client.get("/ui/documents")

        assert response.status_code == 200
        assert "1924 Postcard" in response.text
        assert "postcard" in response.text
        assert "PC-001" in response.text

    def test_document_create_page_renders_form(self, app_client):
        _, client = app_client

        response = client.get("/ui/documents/new")

        assert response.status_code == 200
        assert "Create Document" in response.text
        assert "Document name" in response.text
        assert "Author (Person)" in response.text

    @pytest.mark.asyncio
    async def test_document_detail_page_renders_bento_grid_and_metadata(
        self, app_client, seed_person_and_document
    ):
        _, client = app_client
        doc_id, _ = seed_person_and_document

        response = client.get(f"/ui/documents/{doc_id}")

        assert response.status_code == 200
        assert "Letter from Hig" in response.text
        assert "ZC-1924-001" in response.text
        assert "Zenna Cochran" in response.text
        assert "Edit Document" in response.text

    @pytest.mark.asyncio
    async def test_document_jobs_page_renders_job_links(self, app_client):
        _, client = app_client

        async with session_scope() as session:
            doc = Document(name="Doc With Job", document_type="letter")
            session.add(doc)
            await session.flush()

            job = Job(document_id=doc.id)
            session.add(job)
            await session.commit()
            doc_id = str(doc.id)
            job_id = str(job.id)

        response = client.get(f"/ui/documents/{doc_id}/jobs")

        assert response.status_code == 200
        assert "Jobs for Doc With Job" in response.text
        assert f"Job ID: {job_id}" in response.text

    @pytest.mark.asyncio
    async def test_document_edit_page_prefills_existing_values(
        self, app_client, seed_person_and_document
    ):
        _, client = app_client
        doc_id, _ = seed_person_and_document

        response = client.get(f"/ui/documents/{doc_id}/edit")

        assert response.status_code == 200
        assert "Edit Document Record" in response.text
        assert "Letter from Hig" in response.text
        assert "ZC-1924-001" in response.text

    @pytest.mark.asyncio
    async def test_document_delete_page_blocks_deletion_when_dependencies_exist(
        self, app_client
    ):
        _, client = app_client

        async with session_scope() as session:
            doc = Document(name="Doc With Source", document_type="letter")
            session.add(doc)
            await session.flush()

            source = Source(
                document_id=doc.id,
                page_number=1,
                upload_name="page_1.png",
                filename="page_1.png",
                file_path="/tmp/page_1.png",
            )
            session.add(source)
            await session.commit()
            doc_id = str(doc.id)

        response = client.get(f"/ui/documents/{doc_id}/delete")

        assert response.status_code == 200
        assert "Delete Document" in response.text
        assert "Delete is blocked because related records exist." in response.text
        assert "Dependencies present: Sources" in response.text

    @pytest.mark.asyncio
    async def test_document_delete_page_allows_unlinked_document_deletion(self, app_client):
        _, client = app_client

        async with session_scope() as session:
            doc = Document(name="Orphan Document", document_type="note")
            session.add(doc)
            await session.commit()
            doc_id = str(doc.id)

        response = client.get(f"/ui/documents/{doc_id}/delete")

        assert response.status_code == 200
        assert "Delete Document" in response.text
        assert "Delete document permanently" in response.text
        assert "Delete is blocked" not in response.text