# retoor <retoor@molodetz.nl>

from devplacepy.services.jobs.deepsearch.pdf import extract_pdf_text, is_pdf


def _make_pdf(message: str) -> bytes:
    content = b"BT /F1 24 Tf 72 720 Td (" + message.encode("latin-1") + b") Tj ET"
    bodies = [
        b"<< /Type /Catalog /Pages 2 0 R >>",
        b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
        b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
        b"/Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>",
        b"<< /Length " + str(len(content)).encode() + b" >>\nstream\n" + content + b"\nendstream",
        b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
    ]
    pdf = bytearray(b"%PDF-1.4\n")
    offsets: list[int] = []
    for index, body in enumerate(bodies, start=1):
        offsets.append(len(pdf))
        pdf += str(index).encode() + b" 0 obj\n" + body + b"\nendobj\n"
    xref_pos = len(pdf)
    pdf += b"xref\n0 " + str(len(bodies) + 1).encode() + b"\n"
    pdf += b"0000000000 65535 f \n"
    for offset in offsets:
        pdf += ("%010d 00000 n \n" % offset).encode()
    pdf += b"trailer\n<< /Size " + str(len(bodies) + 1).encode() + b" /Root 1 0 R >>\n"
    pdf += b"startxref\n" + str(xref_pos).encode() + b"\n%%EOF"
    return bytes(pdf)


def test_is_pdf_detects_content_type():
    assert is_pdf("application/pdf", "https://example.com/file", b"") is True
    assert is_pdf("application/x-pdf; charset=binary", "https://example.com/file", b"") is True


def test_is_pdf_detects_url_suffix():
    assert is_pdf("text/html", "https://example.com/report.PDF", b"<html>") is True
    assert is_pdf("", "https://example.com/docs/spec.pdf?v=2", b"") is True


def test_is_pdf_detects_magic_bytes():
    assert is_pdf("application/octet-stream", "https://example.com/download", b"%PDF-1.7") is True


def test_is_pdf_rejects_html():
    assert is_pdf("text/html", "https://example.com/page", b"<!DOCTYPE html>") is False


def test_extract_pdf_text_returns_content():
    title, text = extract_pdf_text(_make_pdf("Hello DeepSearch PDF indexing"))
    assert "Hello DeepSearch PDF indexing" in text


def test_extract_pdf_text_rejects_non_pdf():
    assert extract_pdf_text(b"this is not a pdf at all") == ("", "")
    assert extract_pdf_text(b"") == ("", "")


def test_extract_pdf_text_handles_corrupt_pdf():
    title, text = extract_pdf_text(b"%PDF-1.4\ngarbage that is not valid pdf structure")
    assert title == ""
    assert text == ""
