"""Discord — one quirk + one already-default.

post_url: gallery-dl's `url` is the CDN attachment URL. The "permalink"
for a Discord message uses the (server, channel, message) triple via
`discord.com/channels/<server>/<channel>/<message>`. Note that
permalinks are only resolvable for users in the same server — public
access doesn't work — but the URL is still useful to the operator
in-app.

Description body is in `message` not `content`. That's already covered
by the default description chain in base.py (DEFAULT_DESCRIPTION_KEYS
ends with `message`). No description_keys override needed.
"""

from .base import GD_DEFAULTS, PlatformInfo, str_id_value


def derive_post_url(data: dict) -> str | None:
    sid = str_id_value(data.get("server_id"))
    cid = str_id_value(data.get("channel_id"))
    mid = str_id_value(data.get("message_id"))
    if sid and cid and mid:
        return f"https://discord.com/channels/{sid}/{cid}/{mid}"
    return None


INFO = PlatformInfo(
    key="discord",
    name="Discord",
    description="Download attachments from Discord channels",
    auth_type="token",
    requires_auth=True,
    url_pattern=r"^https?://(www\.)?discord\.com/channels/",
    url_examples=["https://discord.com/channels/123456789/987654321"],
    default_config={**GD_DEFAULTS, "content_types": ["all"]},
    notes="Requires Discord user token (not bot token).",
    derive_post_url=derive_post_url,
)
