# app.py

import os
import json
import streamlit as st

try:
    from google import genai
    from google.genai import types
except ImportError:
    genai = None
    types = None

# --------------------- Gemini Client ---------------------
class GeminiClient:
    def __init__(self):
        self.project =  ""
        self.location =  ""

        if not self.project:
            st.error("Set GOOGLE_CLOUD_PROJECT environment variable before running")
            st.stop()

        if genai is None:
            st.error("Please install the google-generativeai package to use Gemini model")
            st.stop()

        self.client = genai.Client(
            vertexai=True,
            project=self.project,
            location=self.location,
        )

    def generate(self, system_prompt: str, user_content: str) -> str:
        """
        Generate chat completion using Vertex AI Gemini model.
        Since Vertex AI Gemini doesn't support 'chat.completions' yet as in OpenAI,
        we simulate a chat prompt by concatenating system and user messages.
        """
        # Combine prompts into one message
        full_message = system_prompt + "\n\n" + user_content
        
        # Call the model: use generate_content, note the model name may require update
        response = self.client.models.generate_content(
            model='gemini-2.0-flash-001',
            contents=full_message
        )
        # Return the generated text
        return response.candidates[0].content.parts[0].text


# --------------------- FUNCTIONS ---------------------
def generate_summary(client: GeminiClient, data: dict) -> str:
#     system_prompt = """Analyze the JSON order activity log below and create a structured summary. Focus on:  
# 1. **Final Outcome**: Clearly state if the order was *Cancelled* or *Returned* or anything else (with reasons from the data and outcome and last recorded activity).  
# 2. **Order Timeline Overview** (creation timestamp, final status, key users/teams involved)  
# 3. **Critical Events**: Chronological breakdown of(give in the sturcture as provided):  
#    - Fraud checks (declines, locks, emails)  
#    - Payment actions (authorization, voids)  
#    - Cancellation/return triggers (e.g., "Order Cancelled" descriptions)  
#    - Post-resolution access (e.g., coaching/reviews).  
# 4. **Key Notes** (patterns, automation flags, unusual access patterns, or notable reasons)    
# 5. **Key Evidence**: Specific JSON entries proving the outcome (e.g., "Cancelled due to DECLINED: Fraud").  

# **Structure Requirements**:  
# - Use markdown headers and bullet points.  
# - Differentiate between system automation (e.g., "DOT COM") and human actions.  
# - Group repeated events (e.g., multiple "Coaching" logs).  
# - Highlight the *exact reason* for cancellation/return from the "text" or "description" fields.  

# **Rules**:  
# - If "Cancelled" appears in any "description" or "text" field, declare it as the outcome.  
# - If "Returned" is not explicitly mentioned, assume cancellation.  
# - Never guess – use only the provided JSON for conclusions."""

    system_prompt = """Analyze the JSON order activity log below and produce a structured, detailed summary report strictly based on the data provided. Your analysis must follow these guidelines and structure:

# 1. Final Outcome
- Clearly state the final status of the order: **Cancelled**, **Returned**, or another status.
- Base the conclusion solely on explicit mentions of "Cancelled" or "Returned" found in any "description" or "text" fields.
- If "Returned" appears, specify the exact reason for the return as stated in the JSON.
- If "Cancelled" is found and "Returned" is not, declare the order Cancelled.
- Do not infer or guess beyond the explicit data.

# 2. Order Timeline Overview
- Provide the **order creation timestamp**, formatted as MM/DD/YYYY.
- State the **final status** of the order.
- List the **key users, teams, or system automation agents** involved, differentiating human actors from automated systems (e.g., "DOT COM").
- Briefly summarize the main flow of actions/events across the order lifecycle.

# 3. Critical Events (Chronological Breakdown)
Organize and list events chronologically by date (MM/DD/YYYY), grouped as follows:

- **Fraud Checks**
  - Include all declines, fraud holds, account locks, fraud alert emails, or related system flags.
  - Specify whether each event was initiated by system automation or a human user.

- **Payment Actions**
  - List events such as authorizations, voids, failed transactions, chargebacks, or refunds.
  
- **Customer Issues Reported**
  - Document all customer-reported issues, including dates and descriptions as recorded in the JSON.

- **Claims and Risk Reviews**
  - Detail any claims raised, risk assessment activities, or reviews by risk or claims teams.
  
- **Cancellation/Return Triggers**
  - Highlight the exact events that triggered cancellation or return, quoting relevant "description" or "text" fields.

- **Post-resolution Access**
  - Include coaching, user reviews, follow-up notes, or other post-resolution activity logs.
  - Group multiple coaching or review events logically together.

# 4. Key Notes
- Identify any suspicious patterns such as:
  - Frequent or unusual automated system flags.
  - Multiple coaching sessions or repeated reviews.
  - Any unusual or notable user behaviors or system interventions.
- Call out any automation flags and note if events are clearly system-generated vs. human actions.

# 5. Key Evidence
- Provide specific JSON entries verbatim (quoting "description" or "text") that directly support your conclusion about the final outcome.
- Clearly highlight the cancellation or return reason stated within these entries.

---

# Formatting Requirements:
- Use Markdown headers and bullet points for readability.
- Differentiate system automation actions (e.g., "DOT COM," "System") from human operator actions (specific users or team names).
- Use normal date format (MM/DD/YYYY) consistently for all timestamps.
- Group repeated events under combined bullet points with summaries.

---

# Important Rules:
- If any "description" or "text" field contains **"Cancelled"**, classify the order as **Cancelled**.
- If "Returned" is present with reasons, classify order as **Returned** and provide the explicit reason.
- Avoid assumptions and only conclude based on explicit JSON data.

---

Use this prompt verbatim when processing JSON order activity logs to generate insightful, fact-based summaries highlighting order lifecycle, customer interactions, claims and risk assessments, fraud and payment activities, and the conclusive final outcome. """

    user_content = json.dumps(data, indent=2)
    
    try:
        return client.generate(system_prompt=system_prompt, user_content=user_content)
    except Exception as e:
        return f"Error generating summary: {str(e)}"


# --------------------- STREAMLIT UI ---------------------
st.set_page_config("Macy's Notes Analyzer with Vertex AI Gemini", layout="wide")
st.title("📑 Macy's Notes Analysis — Direct LLM Processing (Vertex AI Gemini)")

uploaded_file = st.file_uploader("Upload Macy's JSON Notes", type="json")

if uploaded_file:
    try:
        data = json.load(uploaded_file)
        
        client = GeminiClient()  # Initialize Vertex AI client
        
        with st.spinner("🔍 Analyzing documents..."):
            summary = generate_summary(client, data)
            
        st.success("✅ Analysis Complete!")
        
        # Display formatted summary
        st.markdown("## 📝 Comprehensive Summary")
        st.markdown(summary)
                
    except json.JSONDecodeError:
        st.error("❌ Invalid JSON file format")
    except Exception as e:
        st.error(f"❌ Processing error: {str(e)}")

# Optional: Add original data preview
if uploaded_file and st.checkbox("Show raw data preview"):
    uploaded_file.seek(0)
    st.json(json.load(uploaded_file))

in the code can you tell me how many token we are sending and how many tokes we are getting on frontend itself also tell me time to get the response