# 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.

"""Tests for the database playground scenarios."""

from typing import Any, override

from django.contrib.auth.hashers import check_password

from debusine.artifacts.models import ArtifactCategory
from debusine.db.context import context
from debusine.db.models import Scope, Workspace
from debusine.db.playground import scenarios
from debusine.db.playground.playground import Playground
from debusine.test.django import TestCase
from debusine.utils import extract_generic_type_arguments


class ScenarioTestBase[S: scenarios.Scenario](TestCase):
    """Base class for scenario tests."""

    scenario_cls: type[S]
    built_scenario: S | None
    scenario_kwargs: dict[str, Any]

    @override
    def setUp(self) -> None:
        """Set up backend for lazily building a scenario."""
        super().setUp()
        self.built_scenario = None
        self.scenario_kwargs = {}

    def build_scenario(self, **kwargs: Any) -> None:
        """Build the scenario for the current test."""
        self.built_scenario = self.scenario_cls(**kwargs)
        self.playground.build_scenario(
            self.built_scenario,
            set_current=kwargs.get("set_current", False),
        )

    @property
    def scenario(self) -> S:
        """Return the scenario to be tested, building it if needed."""
        if self.built_scenario is None:
            self.build_scenario()
        assert self.built_scenario is not None
        return self.built_scenario

    @override
    def __init_subclass__(cls, **kwargs: Any) -> None:
        """Set scenario_cls from the type argument."""
        super().__init_subclass__(**kwargs)
        [cls.scenario_cls] = extract_generic_type_arguments(
            cls, ScenarioTestBase
        )


class DefaultScopeUserTestBase[DSU: scenarios.DefaultScopeUser](
    ScenarioTestBase[DSU]
):
    """Test DefaultScopeUser-derived scenario."""

    def test_members(self) -> None:
        """Ensure we get the default members."""
        self.assertEqual(self.scenario.scope.name, "debusine")
        self.assertEqual(self.scenario.user.username, "playground")

    def test_set_context(self) -> None:
        """Test setting in context."""
        self.build_scenario(set_current=True)
        self.assertEqual(context.scope, self.scenario.scope)
        self.assertEqual(context.user, self.scenario.user)

    def test_scope_owners(self) -> None:
        """Test scope_owners."""
        group = self.scenario.scope_owners
        self.assertQuerySetEqual(group.users.all(), [])
        self.assertEqual(
            group.scope_roles.get(resource=self.scenario.scope).role,
            Scope.Roles.OWNER,
        )

    def test_assign_role(self) -> None:
        self.assertQuerySetEqual(self.scenario.scope.roles.all(), [])
        with self.scenario.assign_role(self.scenario.scope, Scope.Roles.OWNER):
            assignment = self.scenario.scope.roles.get()
            self.assertEqual(assignment.resource, self.scenario.scope)
            self.assertEqual(assignment.role, Scope.Roles.OWNER)
            self.assertQuerySetEqual(
                assignment.group.users.all(), [self.scenario.user]
            )
        self.assertQuerySetEqual(self.scenario.scope.roles.all(), [])

    def test_build_with_different_defaults(self) -> None:
        other_playground = Playground(
            default_workspace_name="other", default_username="other"
        )
        self.other_scenario = self.scenario_cls()
        other_playground.build_scenario(self.other_scenario)

        def cleanup() -> None:
            del self.other_scenario

        self.addCleanup(cleanup)

        # The scope name is not yet customizable in the playground
        self.assertEqual(self.other_scenario.scope.name, "debusine")
        self.assertEqual(self.other_scenario.user.username, "other")


class DefaultScopeUserTests(
    DefaultScopeUserTestBase[scenarios.DefaultScopeUser]
):
    """Test DefaultScopeUser scenario."""

    @override
    def test_set_context(self) -> None:
        super().test_set_context()
        self.assertIsNone(context.workspace)


class DefaultScopeUserAPITests(
    DefaultScopeUserTestBase[scenarios.DefaultScopeUserAPI]
):
    """Test DefaultScopeUserAPI scenario."""

    @override
    def test_members(self) -> None:
        super().test_members()
        self.assertEqual(self.scenario.user_token.user, self.scenario.user)


class DefaultContextTestBase[DC: scenarios.DefaultContext](
    DefaultScopeUserTestBase[DC]
):
    """Test DefaultContext-derived scenario."""

    @override
    def test_members(self) -> None:
        super().test_members()
        self.assertEqual(self.scenario.workspace.name, "System")
        self.assertTrue(self.scenario.workspace.public)

    @override
    def test_set_context(self) -> None:
        super().test_set_context()
        self.assertEqual(context.workspace, self.scenario.workspace)

    def test_workspace_owners(self) -> None:
        """Test scope_owners."""
        group = self.scenario.workspace_owners
        self.assertQuerySetEqual(group.users.all(), [])
        self.assertEqual(
            group.workspace_roles.get(resource=self.scenario.workspace).role,
            Workspace.Roles.OWNER,
        )

    @override
    def test_build_with_different_defaults(self) -> None:
        super().test_build_with_different_defaults()
        self.assertEqual(self.other_scenario.workspace.name, "other")
        self.assertEqual(
            self.other_scenario.default_task_configuration_collection.workspace,
            self.other_scenario.workspace,
        )


class DefaultContextTests(DefaultContextTestBase[scenarios.DefaultContext]):
    """Test DefaultContext scenario."""


class DefaultContextAPITests(
    DefaultContextTestBase[scenarios.DefaultContextAPI]
):
    """Test DefaultContextAPI scenario."""

    @override
    def test_members(self) -> None:
        super().test_members()
        self.assertEqual(self.scenario.user_token.user, self.scenario.user)


class DefaultSbuildTestBase[DS: scenarios.DefaultSbuild](
    DefaultContextTestBase[DS]
):
    """Test DefaultSbuild-derived scenario."""

    @override
    def test_members(self) -> None:
        super().test_members()
        self.assertEqual(
            self.scenario.source.category, ArtifactCategory.SOURCE_PACKAGE
        )
        self.assertEqual(self.scenario.environment_sid.data["codename"], "sid")
        self.assertEqual(
            self.scenario.environment_sid.category,
            ArtifactCategory.SYSTEM_TARBALL,
        )


class DefaultSbuildTests(DefaultSbuildTestBase[scenarios.DefaultSbuild]):
    """Test DefaultSbuild scenario."""


class DefaultSbuildAPITests(DefaultSbuildTestBase[scenarios.DefaultSbuildAPI]):
    """Test DefaultSbuildAPI scenario."""

    @override
    def test_members(self) -> None:
        super().test_members()
        self.assertEqual(self.scenario.user_token.user, self.scenario.user)


class UIPlaygroundTests(DefaultContextTestBase[scenarios.UIPlayground]):
    """Test UIPlayground scenario."""

    @override
    def test_members(self) -> None:
        """Ensure we get the default members."""
        self.assertTrue(
            check_password("playground", self.scenario.user.password)
        )
        # TODO: the scenario is kind of improvised and could use a dust-off to
        # make it state of the art with how Debusine actually work. When that
        # is done, the resulting expectations can be encoded here


class PackageBuildLogTests(DefaultContextTestBase[scenarios.PackageBuildLog]):
    """Test PackageBuildLog scenario."""

    @override
    def test_members(self) -> None:
        """Ensure we get the default members."""
        self.assertEqual(self.scenario.workspace.name, "System")
        self.assertTrue(self.scenario.workspace.public)


del DefaultScopeUserTestBase
del DefaultContextTestBase
del DefaultSbuildTestBase
