"""Repository protocol for Workspace operations."""

from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING, Protocol

if TYPE_CHECKING:
    from uuid import UUID

    from noteflow.domain.identity import (
        Workspace,
        WorkspaceMembership,
        WorkspaceRole,
        WorkspaceSettings,
    )


class WorkspaceRepository(Protocol):
    """Repository protocol for Workspace operations."""

    async def get(self, workspace_id: UUID) -> Workspace | None:
        """Get workspace by ID.

        Args:
            workspace_id: Workspace UUID.

        Returns:
            Workspace if found, None otherwise.
        """
        ...

    async def get_by_slug(self, slug: str) -> Workspace | None:
        """Get workspace by slug.

        Args:
            slug: Workspace slug.

        Returns:
            Workspace if found, None otherwise.
        """
        ...

    async def get_default_for_user(self, user_id: UUID) -> Workspace | None:
        """Get the default workspace for a user.

        Args:
            user_id: User UUID.

        Returns:
            Default workspace if exists, None otherwise.
        """
        ...

    async def create(
        self,
        workspace_id: UUID,
        name: str,
        owner_id: UUID,
        slug: str | None = None,
        is_default: bool = False,
        settings: WorkspaceSettings | None = None,
    ) -> Workspace:
        """Create a new workspace.

        Args:
            workspace_id: UUID for the new workspace.
            name: Workspace name.
            owner_id: User UUID of the owner.
            slug: Optional URL slug.
            is_default: Whether this is the user's default workspace.
            settings: Optional workspace settings.

        Returns:
            Created workspace.
        """
        ...

    async def update(self, workspace: Workspace) -> Workspace:
        """Update an existing workspace.

        Args:
            workspace: Workspace with updated fields.

        Returns:
            Updated workspace.

        Raises:
            ValueError: If workspace does not exist.
        """
        ...

    async def delete(self, workspace_id: UUID) -> bool:
        """Delete a workspace.

        Args:
            workspace_id: Workspace UUID.

        Returns:
            True if deleted, False if not found.
        """
        ...

    async def list_for_user(
        self,
        user_id: UUID,
        limit: int = 50,
        offset: int = 0,
    ) -> Sequence[Workspace]:
        """List workspaces a user is a member of.

        Args:
            user_id: User UUID.
            limit: Maximum workspaces to return.
            offset: Pagination offset.

        Returns:
            List of workspaces.
        """
        ...

    async def get_membership(
        self,
        workspace_id: UUID,
        user_id: UUID,
    ) -> WorkspaceMembership | None:
        """Get a user's membership in a workspace.

        Args:
            workspace_id: Workspace UUID.
            user_id: User UUID.

        Returns:
            Membership if user is a member, None otherwise.
        """
        ...

    async def add_member(
        self,
        workspace_id: UUID,
        user_id: UUID,
        role: WorkspaceRole,
    ) -> WorkspaceMembership:
        """Add a user to a workspace.

        Args:
            workspace_id: Workspace UUID.
            user_id: User UUID.
            role: Role to assign.

        Returns:
            Created membership.
        """
        ...

    async def update_member_role(
        self,
        workspace_id: UUID,
        user_id: UUID,
        role: WorkspaceRole,
    ) -> WorkspaceMembership | None:
        """Update a member's role in a workspace.

        Args:
            workspace_id: Workspace UUID.
            user_id: User UUID.
            role: New role.

        Returns:
            Updated membership if found, None otherwise.
        """
        ...

    async def remove_member(
        self,
        workspace_id: UUID,
        user_id: UUID,
    ) -> bool:
        """Remove a user from a workspace.

        Args:
            workspace_id: Workspace UUID.
            user_id: User UUID.

        Returns:
            True if removed, False if not found.
        """
        ...

    async def list_members(
        self,
        workspace_id: UUID,
        limit: int = 100,
        offset: int = 0,
    ) -> Sequence[WorkspaceMembership]:
        """List all members of a workspace.

        Args:
            workspace_id: Workspace UUID.
            limit: Maximum members to return.
            offset: Pagination offset.

        Returns:
            List of memberships.
        """
        ...
