# Written by retoor@molodetz.nl

# This code defines an async class-based view called StatusView for handling HTTP GET requests. It fetches user details and their associated channel memberships from a database and returns a JSON response with user information if the user is logged in.

# The code uses an imported module `BaseView`. There are dependencies on the `snek.system.view` module which provides the BaseView class.

# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from snek.system.view import BaseView


class StatusView(BaseView):
    async def get(self):
        memberships = []
        user = {}

        user_id = self.session.get("uid")
        if user_id:
            user = await self.app.services.user.get(uid=user_id)
            if not user:
                return await self.json_response({"error": "User not found"}, status=404)

            async for model in self.app.services.channel_member.find(
                user_uid=user_id, deleted_at=None, is_banned=False
            ):
                channel = await self.app.services.channel.get(uid=model["channel_uid"])
                memberships.append(
                    {
                        "name": channel["label"],
                        "description": model["description"],
                        "user_uid": model["user_uid"],
                        "is_moderator": model["is_moderator"],
                        "is_read_only": model["is_read_only"],
                        "is_muted": model["is_muted"],
                        "is_banned": model["is_banned"],
                        "channel_uid": model["channel_uid"],
                        "uid": model["uid"],
                    }
                )
            user = {
                "username": user["username"],
                "email": user["email"],
                "nick": user["nick"],
                "uid": user["uid"],
                "color": user["color"],
                "memberships": memberships,
            }

        return await self.json_response(
            {
                "user": user,
                "cache": await self.app.cache.create_cache_key(
                    self.app.cache.cache, None
                ),
            }
        )
