import streamlit as st
import requests

st.title("Revive Campaign AI Analyzer")

campaign_id = 1

if st.button("Analyze Campaign"):

    try:

        response = requests.get(
            f"http://127.0.0.1:8000/campaign/{campaign_id}"
        )

        data = response.json()

        # Campaign Metrics
        st.subheader("Campaign Metrics")
        st.json(data["campaign_data"])

        # AI Analysis
        st.subheader("AI Analysis")
        st.write(data["ai_analysis"])

        # Health Score
        health = data["health"]

        st.metric(
            "Campaign Health Score",
            health["health_score"]
        )

        score = health["health_score"]

        # Health Status
        if score >= 80:
            st.success(f"Health Score: {score}")

        elif score >= 60:
            st.warning(f"Health Score: {score}")

        else:
            st.error(f"Health Score: {score}")

        # Breakdown
        st.subheader("Health Score Breakdown")
        st.json(health["breakdown"])

        # Rules
        datarules = data["rules"]

        st.subheader("Optimization Alerts")

        for rule in datarules:

            severity = rule["severity"]

            message = (
                f"{rule['issue']} → "
                f"{rule['recommendation']}"
            )

            if severity == "HIGH":

                st.error(message)

            elif severity == "MEDIUM":

                st.warning(message)

            else:

                st.info(message)
        #Recommendations        
        st.subheader("AI Recommendations")

        recommendations = data["recommendations"]

        for rec in recommendations:

            priority = rec["priority"]

            message = (
                f"{rec['action']} "
                f"({rec['reason']})"
            )

            if priority == "HIGH":

                st.error(message)

            elif priority == "MEDIUM":

                st.warning(message)

            else:

                st.info(message)
    except Exception as e:

        st.error(f"Error: {e}")