"""
Jira credentials authentication for Jira vein.
"""

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


@dataclass
class JiraCredentials:
    email: str
    token: str


async def get_jira_credentials(
    x_jira_email: str | None = Header(None),
    x_jira_token: str | None = Header(None),
) -> JiraCredentials:
    """
    Dependency that extracts Jira credentials from headers or falls back to config.

    - Headers provided → per-request credentials (web demo)
    - No headers → use .env credentials (API/standalone)
    """
    # Use headers if provided (check for non-empty strings)
    if x_jira_email and x_jira_token and x_jira_email.strip() and x_jira_token.strip():
        return JiraCredentials(email=x_jira_email.strip(), token=x_jira_token.strip())

    # Fall back to config
    if settings.jira_email and settings.jira_api_token:
        return JiraCredentials(email=settings.jira_email, token=settings.jira_api_token)

    raise HTTPException(
        status_code=401,
        detail="Missing credentials: provide X-Jira-Email and X-Jira-Token headers, or configure in .env",
    )
