import datetime
import os
from unittest import skipIf

from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings, TestCase
from wagtail.models import Collection

from vbv_lernwelt.media_files.models import ContentDocument

TITLE = "Musterlösung Fahrzeug"


@skipIf(
    os.environ.get("ENABLE_S3_STORAGE_UNIT_TESTS") is None,
    "Only enable tests by setting ENABLE_S3_STORAGE_UNIT_TESTS=1",
)
class TestContentDocumentStorage(TestCase):
    @override_settings(FILE_UPLOAD_STORAGE="s3")
    def setUp(self):
        now_str = str(datetime.datetime.now().strftime("%d-%m-%Y-%H"))
        collection, _ = Collection.objects.get_or_create(name="Root", depth=0)
        document = ContentDocument.objects.create(
            title=TITLE,
            display_text="Musterlösung Fahrzeug",
            description="Musterlösung für den Auftrag Fahrzeug",
            link_display_text="Dokument laden",
            file=SimpleUploadedFile(
                f"testdocument_{now_str}.txt", b"these are the file contents!"
            ),
            collection=collection,
        )
        document.save()

    def tearDown(self):
        for doc in ContentDocument.objects.all():
            doc.file.storage.delete(doc.file.name)
            doc.delete()

    def test_new_document_is_created(self):
        self.assertEqual(ContentDocument.objects.all().count(), 1)
        self.assertEqual(ContentDocument.objects.filter(title=TITLE).count(), 1)

    def test_document_exists_on_s3(self):
        document = ContentDocument.objects.get(title=TITLE)
        self.assertTrue(document.file.storage.exists(document.file.name))

    def test_download_document_from_s3(self):
        document = ContentDocument.objects.get(title=TITLE)
        self.assertEqual(document.file.read(), b"these are the file contents!")

    def test_delete_document_from_s3(self):
        document = ContentDocument.objects.get(title=TITLE)
        document.file.storage.delete(document.file.name)
        document.delete()
        self.assertFalse(document.file.storage.exists(document.file.name))

    @skipIf(
        settings.AWS_S3_FILE_OVERWRITE,
        "This test only works if AWS_S3_FILE_OVERWRITE is False",
    )
    def test_duplicate_title_and_filename(self):
        collection, _ = Collection.objects.get_or_create(name="Root", depth=0)
        document = ContentDocument.objects.create(
            title=TITLE,
            display_text="Musterlösung Fahrzeug",
            description="Musterlösung für den Auftrag Fahrzeug",
            link_display_text="Dokument laden",
            file=SimpleUploadedFile(
                "testdocument.txt", b"these are the file contents! For sure!"
            ),
            collection=collection,
        )

        document2 = ContentDocument.objects.create(
            title=TITLE,
            display_text="Musterlösung Fahrzeug",
            description="Musterlösung für den Auftrag Fahrzeug",
            link_display_text="Dokument laden",
            file=SimpleUploadedFile(
                "testdocument.txt", b"these are the file contents! But different!"
            ),
            collection=collection,
        )

        self.assertEqual(
            document.file.read(), b"these are the file contents! For sure!"
        )
        self.assertEqual(
            document2.file.read(), b"these are the file contents! But different!"
        )
        self.assertTrue(document.file.name != document2.file.name)
