import shutil
import tempfile
from pathlib import Path

import magic

from tests.conftest import SAMPLE_DIR
from tika_client.client import TikaClient


class TestParseFormatted:
    """
    Test the Tika endpoint for returning HTML formatted content
    """

    def test_parse_docx_from_file_as_html(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.docx"
        resp = tika_client.tika.as_html.from_file(test_file, magic.from_file(str(test_file), mime=True))

        assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        assert "<body><p>This is an DOCX test document, also made September 14, 2022</p>\n</body>" in resp.content
        assert resp.content_length == 6424

    def test_parse_doc_from_file_as_html(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.doc"
        resp = tika_client.tika.as_html.from_file(test_file, magic.from_file(str(test_file), mime=True))

        assert resp.type == "application/msword"
        assert (
            "body><p>This is a test document, saved in the older .doc format for Word documents (but created in Google Drive)</p>\n</body>"  # noqa: E501
            in resp.content
        )
        assert resp.content_length == 23739
        assert resp.character_count == 90
        assert resp.page_count == 1
        assert resp.revision == 1
        assert resp.last_author == "cloudconvert_4"

    def test_parse_docx_no_mime_from_file_as_html(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.docx"
        resp = tika_client.tika.as_html.from_file(test_file)

        assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        assert "<body><p>This is an DOCX test document, also made September 14, 2022</p>\n</body>" in resp.content

    def test_parse_microsoft_word_docx_from_file_as_html(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "microsoft-sample.docx"
        resp = tika_client.tika.as_html.from_file(test_file, magic.from_file(str(test_file), mime=True))

        assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        assert (
            "<body><p>This is a sample document, generated by Microsoft Office on Wednesday, May 17, 2023.</p>\n<p>It is in English.</p>\n</body>"  # noqa: E501
            in resp.content
        )

    def test_parse_odt_from_file_as_html(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.odt"
        resp = tika_client.tika.as_html.from_file(test_file, magic.from_file(str(test_file), mime=True))

        assert resp.type == "application/vnd.oasis.opendocument.text"
        assert "<body><p>This is an ODT test document, created September 14, 2022</p>\n</body>" in resp.content

    def test_parse_docx_from_buffer_as_html(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.docx"
        content = test_file.read_bytes()
        resp = tika_client.tika.as_html.from_buffer(content, magic.from_buffer(content, mime=True))

        assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        assert "<body><p>This is an DOCX test document, also made September 14, 2022</p>\n</body>" in resp.content
        assert resp.content_length == 6183


class TestParsePlain:
    def test_parse_docx_from_file_as_text(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.docx"

        resp = tika_client.tika.as_text.from_file(test_file, magic.from_file(str(test_file), mime=True))

        assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        assert "This is an DOCX test document, also made September 14, 2022" in resp.content

    def test_parse_odt_from_file_as_text(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.odt"

        resp = tika_client.tika.as_text.from_file(test_file, magic.from_file(str(test_file), mime=True))

        assert resp.type == "application/vnd.oasis.opendocument.text"
        assert "This is an ODT test document, created September 14, 2022" in resp.content


class TestParseContentPlain:
    def test_parse_docx_from_bytes_buffer(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.docx"
        buffer = test_file.read_bytes()

        resp = tika_client.tika.as_text.from_buffer(buffer)

        assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        assert "This is an DOCX test document, also made September 14, 2022" in resp.content

    def test_parse_odt_from_bytes_buffer(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.odt"
        buffer = test_file.read_bytes()

        resp = tika_client.tika.as_text.from_buffer(buffer)

        assert resp.type == "application/vnd.oasis.opendocument.text"
        assert "This is an ODT test document, created September 14, 2022" in resp.content

    def test_parse_odt_from_bytes_buffer_with_mime(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.odt"
        buffer = test_file.read_bytes()

        resp = tika_client.tika.as_text.from_buffer(buffer, "application/vnd.oasis.opendocument.text")

        assert resp.type == "application/vnd.oasis.opendocument.text"
        assert "This is an ODT test document, created September 14, 2022" in resp.content

    def test_html_document_from_string_buffer(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.html"
        buffer = test_file.read_text()

        resp = tika_client.tika.as_text.from_buffer(buffer)

        assert resp.type == "text/html; charset=UTF-8"
        assert resp.parsers == ["org.apache.tika.parser.DefaultParser", "org.apache.tika.parser.html.HtmlParser"]
        assert "Hello world! This is HTML5 content in a file for" in resp.data["X-TIKA:content"]
        assert resp.data["dc:title"] == "This Is A Test"
        assert resp.data["description"] == "A sample HTML file"


class TestParseContentCompress:
    def test_parse_odt_from_bytes_buffer_compress(self, tika_client_compressed: TikaClient):
        test_file = SAMPLE_DIR / "test-document-images.odt"
        buffer = test_file.read_bytes()

        resp = tika_client_compressed.tika.as_text.from_buffer(buffer)

        assert resp.type == "application/vnd.oasis.opendocument.text"


class TestFilenameContentDisposition:
    def test_non_ascii_filename(self, tika_client: TikaClient):
        test_file = SAMPLE_DIR / "sample.docx"

        with tempfile.TemporaryDirectory() as temp_dir:
            copy = shutil.copy(
                test_file,
                Path(temp_dir) / "Kostenerstattung für Meldebescheinigung Familienzuschlag.docx",
            )

            resp = tika_client.tika.as_text.from_file(copy, magic.from_file(str(copy), mime=True))

            assert resp.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            assert "This is an DOCX test document, also made September 14, 2022" in resp.content
