from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse
from starlette import status

from src.depends.services import get_service
from src.model.internal.session import UserSession
from src.model.view.inst_emp_csv_download_view_model import \
    InstEmpCsvDownloadViewModel
from src.model.view.inst_emp_csv_upload_view_model import \
    InstEmpCsvUploadViewModel
from src.model.view.master_mainte_menu_view_model import \
    MasterMainteMenuViewModel
from src.model.view.table_override_view_model import TableOverrideViewModel
from src.router.session_router import AuthenticatedRoute
from src.services.batch_status_service import BatchStatusService
from src.services.session_service import set_session
from src.system_var import constants
from src.templates import templates

router = APIRouter()
router.route_class = AuthenticatedRoute

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


@router.get('/masterMainteMenu', response_class=HTMLResponse)
def menu_view(
    request: Request,
    batch_status_service: BatchStatusService = Depends(get_service(BatchStatusService))
):
    session: UserSession = request.session

    # マスタメンテメニューへのアクセス権がない場合、ログアウトさせる
    if session.master_mainte_flg != '1':
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)

    # バッチ処理中の場合、ログアウトさせる
    if batch_status_service.is_batch_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail=constants.LOGOUT_REASON_BATCH_PROCESSING_FOR_MAINTE)
    # dump処理中の場合、ログアウトさせる
    if batch_status_service.is_dump_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=constants.LOGOUT_REASON_BACKUP_PROCESSING)

    # 画面表示用のモデル
    menu = MasterMainteMenuViewModel()
    # セッション書き換え
    session.update(
        actions=[
            UserSession.last_access_time.set(UserSession.new_last_access_time()),
            UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
        ]
    )
    set_session(session)
    templates_response = templates.TemplateResponse(
        'masterMainteMenu.html',
        {
            'request': request,
            'menu': menu
        },
        headers={'session_key': session.session_key}
    )
    return templates_response


@router.get('/instEmpCsvUL', response_class=HTMLResponse)
def inst_emp_csv_upload_view(
    request: Request,
    batch_status_service: BatchStatusService = Depends(get_service(BatchStatusService))
):
    session: UserSession = request.session

    # マスタメンテメニューへのアクセス権がない場合、ログアウトさせる
    if session.master_mainte_flg != '1':
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)

    # バッチ処理中の場合、ログアウトさせる
    if batch_status_service.is_batch_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail=constants.LOGOUT_REASON_BATCH_PROCESSING_FOR_MAINTE)
    # dump処理中の場合、ログアウトさせる
    if batch_status_service.is_dump_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=constants.LOGOUT_REASON_BACKUP_PROCESSING)

    # 画面表示用のモデル
    view_model = InstEmpCsvUploadViewModel()
    # セッション書き換え
    session.update(
        actions=[
            UserSession.last_access_time.set(UserSession.new_last_access_time()),
            UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
        ]
    )
    set_session(session)
    templates_response = templates.TemplateResponse(
        'instEmpCsvUL.html',
        {
            'request': request,
            'view': view_model
        },
        headers={'session_key': session.session_key}
    )
    return templates_response


@router.get('/instEmpCsvDL', response_class=HTMLResponse)
def inst_emp_csv_download_view(
    request: Request,
    batch_status_service: BatchStatusService = Depends(get_service(BatchStatusService))
):
    session: UserSession = request.session

    # マスタメンテメニューへのアクセス権がない場合、ログアウトさせる
    if session.master_mainte_flg != '1':
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)

    # バッチ処理中の場合、ログアウトさせる
    if batch_status_service.is_batch_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail=constants.LOGOUT_REASON_BATCH_PROCESSING_FOR_MAINTE)
    # dump処理中の場合、ログアウトさせる
    if batch_status_service.is_dump_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=constants.LOGOUT_REASON_BACKUP_PROCESSING)

    # 画面表示用のモデル
    view_model = InstEmpCsvDownloadViewModel()
    # セッション書き換え
    session.update(
        actions=[
            UserSession.last_access_time.set(UserSession.new_last_access_time()),
            UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
        ]
    )
    set_session(session)
    templates_response = templates.TemplateResponse(
        'instEmpCsvDL.html',
        {
            'request': request,
            'view': view_model
        },
        headers={'session_key': session.session_key}
    )
    return templates_response


@router.get('/tableOverride', response_class=HTMLResponse)
def table_override_view(
    request: Request,
    batch_status_service: BatchStatusService = Depends(get_service(BatchStatusService))
):
    session: UserSession = request.session

    # マスタメンテメニューへのアクセス権がない場合、ログアウトさせる
    if session.master_mainte_flg != '1':
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)

    # バッチ処理中の場合、ログアウトさせる
    if batch_status_service.is_batch_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail=constants.LOGOUT_REASON_BATCH_PROCESSING_FOR_MAINTE)
    # dump処理中の場合、ログアウトさせる
    if batch_status_service.is_dump_processing():
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=constants.LOGOUT_REASON_BACKUP_PROCESSING)

    # 画面表示用のモデル
    view_model = TableOverrideViewModel()
    # セッション書き換え
    session.update(
        actions=[
            UserSession.last_access_time.set(UserSession.new_last_access_time()),
            UserSession.record_expiration_time.set(UserSession.new_record_expiration_time()),
        ]
    )
    set_session(session)
    templates_response = templates.TemplateResponse(
        'tableOverride.html',
        {
            'request': request,
            'view': view_model
        },
        headers={'session_key': session.session_key}
    )
    return templates_response
