# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""UI helpers for artifacts."""

from functools import cached_property
from typing import override

from debusine.artifacts.models import ArtifactCategory
from debusine.db.models import Artifact, FileInArtifact
from debusine.db.models.artifacts import ARTIFACT_CATEGORY_ICON_NAMES
from debusine.web.views import ui_shortcuts
from debusine.web.views.places import (
    PlaceValue,
    ResourceMainPlace,
    ResourceSubPlace,
)
from debusine.web.views.ui.base import UI


class ArtifactUI(UI[Artifact]):
    """UI helpers for Artifact instances."""

    @cached_property
    def label(self) -> str:
        """Return the artifact label."""
        return self.instance.get_label()

    @cached_property
    def place(self) -> ResourceMainPlace[Artifact]:
        """Return a place to show this artifact."""
        return ResourceMainPlace(
            ui=self,
            title=PlaceValue(
                self.label,
                page=f"Artifact {self.label}",
            ),
            url=self.instance.get_absolute_url(),
            icon=ARTIFACT_CATEGORY_ICON_NAMES.get(
                ArtifactCategory(self.instance.category), "file"
            ),
            description=f"{self.instance.category} artifact {self.label}",
            parent=self.instance.workspace.ui(self.request).place,
        )

    @override
    def build_ui_shortcuts(self) -> list[ui_shortcuts.UIShortcut]:
        result = super().build_ui_shortcuts()
        result += [
            ui_shortcuts.create_artifact_view(self.instance),
            ui_shortcuts.create_artifact_download(self.instance),
        ]
        if ArtifactCategory(self.instance.category) == ArtifactCategory.UPLOAD:
            changes_file = self.instance.fileinartifact_set.filter(
                path__endswith=".changes"
            ).first()

            if changes_file is not None:
                result.append(
                    ui_shortcuts.create_upload_changes_download(changes_file)
                )
        return result


class FileInArtifactUI(UI[FileInArtifact]):
    """UI helpers for FileInArtifact instances."""

    @cached_property
    def place(self) -> ResourceMainPlace[FileInArtifact]:
        """Return a place to show this file."""
        title = str(self.instance.path)
        if not self.instance.complete:
            title += " (incomplete)"
        return ResourceMainPlace(
            ui=self,
            title=PlaceValue(str(self.instance.path), page=title),
            url=self.instance.get_absolute_url(),
            parent=self.instance.artifact.ui(self.request).place,
        )

    @cached_property
    def place_raw(self) -> ResourceSubPlace[FileInArtifact]:
        """Return a place to show this file (raw view)."""
        title = str(self.instance.path)
        if not self.instance.complete:
            title += " (incomplete)"
        return ResourceSubPlace(
            ui=self,
            title=PlaceValue(str(self.instance.path), page=title),
            url=self.instance.get_absolute_url_raw(),
            parent=self.instance.artifact.ui(self.request).place,
        )

    @cached_property
    def place_download(self) -> ResourceSubPlace[FileInArtifact]:
        """Return a place to show this file (download view)."""
        title = str(self.instance.path)
        if not self.instance.complete:
            title += " (incomplete)"
        return ResourceSubPlace(
            ui=self,
            title=PlaceValue(str(self.instance.path), page=title),
            url=self.instance.get_absolute_url_download(),
            parent=self.instance.artifact.ui(self.request).place,
        )

    @override
    def build_ui_shortcuts(self) -> list[ui_shortcuts.UIShortcut]:
        result = super().build_ui_shortcuts()
        result += [
            ui_shortcuts.create_file_view(self.instance),
            ui_shortcuts.create_file_view_raw(self.instance),
            ui_shortcuts.create_file_download(self.instance),
        ]
        return result
