from fastapi import APIRouter, Depends, HTTPException, status

from app.api.dependencies.auth import get_access_token
from app.api.dependencies.permissions import require_permission
from app.application.admin.sdk_configs import SdkConfigService
from app.schemas.auth import AuthenticatedUser
from app.schemas.admin import SdkConfigListResponse, SdkConfigRead

router = APIRouter()
service = SdkConfigService()


@router.get("", response_model=SdkConfigListResponse)
async def list_sdk_configs(
    access_token: str = Depends(get_access_token),
    _: AuthenticatedUser = Depends(require_permission("can_manage_sdk_configs")),
) -> SdkConfigListResponse:
    items = await service.list_sdk_configs(access_token=access_token)
    return SdkConfigListResponse(items=items)


@router.get("/{sdk_config_id}", response_model=SdkConfigRead)
async def get_sdk_config(
    sdk_config_id: str,
    access_token: str = Depends(get_access_token),
    _: AuthenticatedUser = Depends(require_permission("can_manage_sdk_configs")),
) -> SdkConfigRead:
    item = await service.get_sdk_config(sdk_config_id, access_token=access_token)
    if not item:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"SDK config '{sdk_config_id}' not found.",
        )
    return item
