"""Slack channel tools — list, create, archive, set topic."""

from slack_mcp_server.client import SlackClient
from slack_mcp_server.config import SlackConfig


def register_channel_tools(app, config: SlackConfig):
    """Register channel tools with the FastMCP app."""

    @app.tool()
    async def slack_list_channels(types: str = "public_channel") -> dict:
        """List channels in the Slack workspace.

        Args:
            types: Channel types to list (comma-separated). Options: 'public_channel', 'private_channel', 'mpim', 'im'
        """
        async with SlackClient(bot_token=config.bot_token, user_token=config.user_token) as client:
            channels = await client.list_channels(types=types)
            return {"success": True, "count": len(channels), "channels": channels}

    @app.tool()
    async def slack_create_channel(name: str, purpose: str = "") -> dict:
        """Create a new Slack channel.

        Args:
            name: Channel name (lowercase, no spaces, max 80 chars, e.g., 'project-updates')
            purpose: Channel purpose/description (optional)
        """
        async with SlackClient(bot_token=config.bot_token, user_token=config.user_token) as client:
            result = await client.create_channel(name=name, purpose=purpose)
            return {"success": True, "channel_id": result["channel_id"], "name": result["name"]}

    @app.tool()
    async def slack_archive_channel(channel: str) -> dict:
        """Archive a Slack channel.

        Args:
            channel: Channel ID to archive (e.g., 'C01234ABCDE')
        """
        async with SlackClient(bot_token=config.bot_token, user_token=config.user_token) as client:
            result = await client.archive_channel(channel=channel)
            return {"success": True, "archived": True, "channel": channel}

    @app.tool()
    async def slack_set_channel_topic(channel: str, topic: str) -> dict:
        """Set the topic for a Slack channel.

        Args:
            channel: Channel ID (e.g., 'C01234ABCDE')
            topic: New topic text for the channel
        """
        async with SlackClient(bot_token=config.bot_token, user_token=config.user_token) as client:
            result = await client.set_channel_topic(channel=channel, topic=topic)
            return {"success": True, "channel": channel, "topic": topic}
