from fastapi import APIRouter, Depends, HTTPException, Request
from starlette import status

from src.depends.services import get_service
from src.logging.get_logger import get_logger
from src.model.internal.session import UserSession
from src.router.session_router import AuthenticatedRoute
from src.services.batch_status_service import BatchStatusService
from src.services.bio_view_service import BioViewService
from src.system_var import constants
from src.templates import templates

router = APIRouter()
router.route_class = AuthenticatedRoute
logger = get_logger('生物由来参照')

#########################
# Views                 #
#########################


@router.get('/BioSearchList')
def bio_view(
    request: Request,
    batch_status_service: BatchStatusService = Depends(get_service(BatchStatusService)),
    bio_service: BioViewService = Depends(get_service(BioViewService))
):
    session: UserSession = request.session
    # バッチ処理中の場合、機能を利用させない
    if batch_status_service.is_batch_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=constants.LOGOUT_REASON_BATCH_PROCESSING)

    logger.debug(f'UserId: {session.user_id}')
    # 検索項目の取得
    bio = bio_service.prepare_bio_view(session)

    # レスポンス
    session_key = session.session_key
    templates_response = templates.TemplateResponse(
        'bioSearchList.html', {
            'request': request,
            'bio': bio,
        },
        headers={'session_key': session_key}
    )
    return templates_response
