# SPDX-FileCopyrightText: 2017-present Tobias Kunze
# SPDX-License-Identifier: AGPL-3.0-only WITH LicenseRef-Pretalx-AGPL-3.0-Terms

import logging
from pathlib import Path

from django.core.files.storage import default_storage
from django_scopes import scopes_disabled

from pretalx.celery_app import app
from pretalx.common.image import process_image
from pretalx.event.models import Event
from pretalx.person.models import User
from pretalx.submission.models import Submission

logger = logging.getLogger(__name__)


@app.task(name="pretalx.process_image")
def task_process_image(*, model: str, pk: int, field: str, generate_thumbnail: bool):
    models = {
        "Event": Event,
        "Submission": Submission,
        "User": User,
    }
    if model not in models:
        return

    with scopes_disabled():
        instance = models[model].objects.filter(pk=pk).first()
        if not instance:
            return

        image = getattr(instance, field, None)
        if not image:
            return

        try:
            process_image(image=image, generate_thumbnail=generate_thumbnail)
        except Exception as e:  # pragma: no cover
            logger.error("Could not process image %s: %s", image.path, e)


@app.task(name="pretalx.cleanup_file")
def task_cleanup_file(*, model: str, pk: int, field: str, path: str):
    models = {
        "Event": Event,
        "Submission": Submission,
        "User": User,
    }
    if model not in models:
        return

    with scopes_disabled():
        instance = models[model].objects.filter(pk=pk).first()
        if not instance:
            return

        file = getattr(instance, field, None)
        if file and file.path == path:
            # The save action that triggered this task did not go through and the file
            # is still in use, so we should not delete it.
            return

        real_path = Path(path)
        if real_path.exists():
            try:
                default_storage.delete(path)
            except OSError:  # pragma: no cover
                logger.error("Deleting file %s failed.", path)
