"""Phase 2.6 — metadata-step completeness + review summary (#13).

The wizard spreads SRS metadata across steps, so a final Review step renders the
full record before submit. The summary is bound client-side; here we assert the
review surface and its labelled fields render, and that a submission carrying the
full field set still succeeds.
"""
from __future__ import annotations

import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import Client
from django.urls import reverse

from apps.repository.documents.models import Document

PDF_BYTES = (
    b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n1 0 obj<</Type/Catalog>>endobj\ntrailer<<>>\n%%EOF\n"
)


@pytest.mark.django_db
def test_wizard_renders_review_step_with_all_fields(public_collection, submitter_user):
    client = Client()
    client.force_login(submitter_user)
    resp = client.get(reverse("repo_documents:submit"))
    assert resp.status_code == 200
    body = resp.content.decode()
    # Stepper gains a sixth "Review" step.
    assert "'Review'" in body
    assert "Review &amp; submit" in body
    # Every SRS metadata facet appears as a labelled summary row.
    for label in ["Title", "Abstract", "Type", "Authors", "Collection", "Licence", "Grant", "Tags"]:
        assert f">{label}</dt>" in body


@pytest.mark.django_db
def test_full_record_submits_from_review(public_collection, submitter_user):
    client = Client()
    client.force_login(submitter_user)
    data = {
        "title": "Complete record",
        "abstract": "Everything filled in.",
        "document_type": Document.DocumentType.THESIS,
        "license_type": Document.License.CC_BY,
        "language": "en",
        "year": 2026,
        "collection": public_collection.pk,
        "authors_text": "Kato, Amina",
        "keywords_text": "",
        "grant_reference": "",
        "file": SimpleUploadedFile("t.pdf", PDF_BYTES, content_type="application/pdf"),
    }
    resp = client.post(reverse("repo_documents:submit"), data=data)
    assert resp.status_code == 302
    assert Document.objects.count() == 1
