import datetime

import streamlit as st


def get_tickets(user: str) -> list[dict[str, str]]:
    return st.session_state.client.get_user_tickets(user)


def tickets() -> None:
    translation = st.session_state.translations[st.session_state.language]["tickets_page"]
    st.title(translation["title"])
    data = get_tickets(str(st.user.email))
    if not data:
        st.markdown(translation["no_tickets"])
    for i, ticket in enumerate(data):
        created = datetime.datetime.fromisoformat(ticket.get("Created", ""))
        with st.expander(f"# **#{ticket['id']} | {ticket['Subject']}**", expanded=(i < 2)):
            st.markdown(f"{translation['created']}: {created.strftime('%Y-%m-%d %H:%M:%S %Z')}")
            st.markdown(f"{translation['status']}: {ticket['Status']}")
            st.link_button(translation["view"], ticket["url"])


if st.session_state.page == 1:
    tickets()
