import os

import factory
from django.conf import settings
from wagtail.models import Collection

from vbv_lernwelt.media_files.models import ContentDocument, UserDocument
from vbv_lernwelt.media_files.tests.media_library_factories import (
    ContentDocumentFactory,
    UserDocumentFactory,
)


def delete_default_documents():
    """deletes all  documents"""
    if "prod" in settings.APP_ENVIRONMENT:
        raise Exception("This command must not be used in production environment")

    ContentDocument.objects.all().delete()
    UserDocument.objects.all().delete()


def create_default_collections():
    root, created = Collection.objects.get_or_create(name="Root", depth=0)


def create_default_content_documents():
    """creates a default document for testing purposes"""

    path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "./tests/test_documents/"
    )

    filename = "Vermittler_Motorfahrzeug_Versicherung_Musterlösung.pdf"
    document = ContentDocumentFactory(
        title="Musterlösung Fahrzeug",
        display_text="Musterlösung Fahrzeug",
        description="Musterlösung für den Auftrag Fahrzeug",
        link_display_text="Dokument laden",
        file=factory.django.FileField(
            from_path=os.path.join(path, filename), filename=filename
        ),
    )
    document.tags.set(("Fahrzeug", "Musterlösung", "Vermittler"))
    document.save()

    filename = "TestExcelSheet.xlsx"
    document = ContentDocumentFactory(
        title="Mustertabelle",
        display_text="Mustertabelle",
        link_display_text="Dokument laden",
        file=factory.django.FileField(
            from_path=os.path.join(path, filename), filename=filename
        ),
    )
    document.tags.set(("Vermittler"))
    document.save()


def create_default_user_documents():
    """creates a default document for testing purposes"""

    path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "./tests/test_documents/"
    )

    filename = "FallanalyseTeststudent.pdf"
    document = UserDocumentFactory(
        title="Lösung Fallanalyse",
        file=factory.django.FileField(
            from_path=os.path.join(path, filename), filename=filename
        ),
    )
    document.save()

    filename = "FallanalyseTeststudent.docx"
    document = UserDocumentFactory(
        title="Lösung Fallanalyse",
        file=factory.django.FileField(
            from_path=os.path.join(path, filename), filename=filename
        ),
    )
    document.save()
