import asyncio
import streamlit as st
from redis_manager import RedisTagManager
REDIS_HOST = "localhost"
REDIS_PORT = 6379
REDIS_DB = 1

async def main():
    # Initialize Redis manager
    tag_manager = RedisTagManager(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)

    # Store previous tags to detect changes
    previous_top_tags = None
    leaderboard_placeholder = st.empty()

    while True:
        top_tags = tag_manager.get_top_tags(5)

        # Only update if tags have changed
        if top_tags != previous_top_tags:
            leaderboard_str = ""
            for tag_name, vote_count in top_tags:
                if vote_count > 0:  # Only show tags with votes
                    leaderboard_str += f"{tag_name} ({vote_count} votes)  \n"

            if not leaderboard_str:
                leaderboard_str = "(no tags requested yet)"
                        
            st.markdown("""
            <style>
            .big-font {
                font-size:50px !important;
            }
            </style>
            """, unsafe_allow_html=True)

            leaderboard_placeholder.markdown(f'<p class="big-font">Top {5} tags:  \n {leaderboard_str.strip()}</p>', unsafe_allow_html=True)

            previous_top_tags = top_tags

        # Small sleep to prevent excessive CPU usage
        await asyncio.sleep(0.1)


# Run the app
if __name__ == "__main__":
    asyncio.run(main())
