"""
Slack credentials authentication for Slack vein.
"""

from dataclasses import dataclass
from fastapi import Header, HTTPException
from .config import settings


@dataclass
class SlackCredentials:
    token: str


async def get_slack_credentials(
    x_slack_token: str | None = Header(None),
) -> SlackCredentials:
    """
    Dependency that extracts Slack token from headers or falls back to config.

    - Header provided → per-request token (web demo)
    - No header → use .env token (API/standalone)
    """
    # Use header if provided
    if x_slack_token and x_slack_token.strip():
        return SlackCredentials(token=x_slack_token.strip())

    # Fall back to config (prefer bot token, then user token)
    if settings.slack_bot_token:
        return SlackCredentials(token=settings.slack_bot_token)
    if settings.slack_user_token:
        return SlackCredentials(token=settings.slack_user_token)

    raise HTTPException(
        status_code=401,
        detail="Missing credentials: provide X-Slack-Token header, or configure in .env",
    )
