"""Helpers to check for apps using deprecated compatibility entries."""

from ...const import AppStage, CoreState
from ...coresys import CoreSys
from ..const import ContextType, IssueType, SuggestionType
from ..data import Issue
from .base import CheckBase


def setup(coresys: CoreSys) -> CheckBase:
    """Check setup function."""
    return CheckDeprecatedArchApp(coresys)


class CheckDeprecatedArchApp(CheckBase):
    """CheckDeprecatedArchApp class for check."""

    @property
    def slug(self) -> str:
        """Return the check slug."""
        return "deprecated_arch_app"

    async def run_check(self) -> None:
        """Run check if not affected by issue."""
        for app in self.sys_apps.installed:
            if app.stage == AppStage.DEPRECATED:
                continue

            if (app.has_deprecated_arch and not app.has_supported_arch) or (
                app.has_deprecated_machine and not app.has_supported_machine
            ):
                self.sys_resolution.create_issue(
                    IssueType.DEPRECATED_ARCH_APP,
                    ContextType.ADDON,
                    reference=app.slug,
                    suggestions=[SuggestionType.EXECUTE_REMOVE],
                )

    async def approve_check(self, issue: Issue) -> bool:
        """Approve check if it is affected by issue."""
        if not issue.reference:
            return False

        app = self.sys_apps.get_local_only(issue.reference)
        return (
            app is not None
            and app.stage != AppStage.DEPRECATED
            and (
                (app.has_deprecated_arch and not app.has_supported_arch)
                or (app.has_deprecated_machine and not app.has_supported_machine)
            )
        )

    @property
    def issue(self) -> IssueType:
        """Return a IssueType enum."""
        return IssueType.DEPRECATED_ARCH_APP

    @property
    def context(self) -> ContextType:
        """Return a ContextType enum."""
        return ContextType.ADDON

    @property
    def states(self) -> list[CoreState]:
        """Return a list of valid states when this check can run."""
        return [CoreState.SETUP, CoreState.RUNNING]
