from fastapi import APIRouter, Depends, HTTPException, status

from app.api.dependencies.permissions import require_permission
from app.application.admin.marketing_cards import MarketingCardService
from app.schemas.auth import AuthenticatedUser
from app.schemas.marketing_card import MarketingCardListResponse, MarketingCardRead

router = APIRouter()
service = MarketingCardService()


@router.get("", response_model=MarketingCardListResponse)
async def list_marketing_cards(
    _: AuthenticatedUser = Depends(require_permission("can_manage_experiments")),
) -> MarketingCardListResponse:
    items = await service.list_marketing_cards()
    return MarketingCardListResponse(items=items)


@router.get("/{card_id}", response_model=MarketingCardRead)
async def get_marketing_card(
    card_id: str,
    _: AuthenticatedUser = Depends(require_permission("can_manage_experiments")),
) -> MarketingCardRead:
    item = await service.get_marketing_card(card_id)
    if not item:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"Marketing card '{card_id}' not found.",
        )
    return item

