import uuid

import streamlit as st

from csirtmu.self_service.client import SelfServiceClient
from csirtmu.self_service.config import AppConfig, BackendConfig, Config
from csirtmu.self_service.ui_helpers import get_sidebar, get_translations


@st.cache_data
def get_config() -> Config:
    return AppConfig.get()


@st.cache_resource
def client(config: BackendConfig) -> SelfServiceClient:
    return SelfServiceClient(config)


st.session_state["translations"] = get_translations()
if "version" not in st.session_state:
    st.session_state["version"] = uuid.uuid4().hex

if not st.user.is_logged_in:
    get_sidebar(True)
    login_page = st.Page("pages/login.py", title="Login", default=True)
    page = st.navigation([login_page], position="hidden")
else:
    get_sidebar()
    translation = st.session_state.translations[st.session_state.language]
    st.session_state["config"] = get_config()
    st.session_state["client"] = client(st.session_state.config.backend)

    main_page = st.Page("pages/main.py", title=translation["sidebar"]["main_page"])
    tickets_page = st.Page("pages/tickets.py", title=translation["sidebar"]["tickets"])
    page = st.navigation({translation["sidebar"]["navigation"]: [main_page, tickets_page]})
    if page.title == translation["sidebar"]["main_page"]:
        st.session_state.page = 0
    else:
        st.session_state.page = 1

page.run()
