import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import os

# Veri yolları
CLEAN_DATA_PATH = 'data/processed/movies_clean.csv'
SCORES_DATA_PATH = 'data/processed/movie_scores.csv'

output_dir = 'web_output'
os.makedirs(output_dir, exist_ok=True)

df_clean = pd.read_csv(CLEAN_DATA_PATH)
df_scores = pd.read_csv(SCORES_DATA_PATH)

# --- HOVER STYLE (Ortak Pop-up Tasarımı) ---
# Burada yazı boyutunu (16) ve kutu içindeki boşlukları artırıyoruz
hover_style = dict(
    bgcolor="rgba(30, 30, 30, 0.9)",
    font_size=16, # Yazıları büyüttük
    font_family="Arial",
    bordercolor="white"
)

# --- CHART 1: Bar Chart (Büyük Pop-up) ---
df_melted = df_clean.melt(id_vars=['Title'], 
                          value_vars=['IMDB_norm', 'RottenTomatoes_norm', 'Metacritic_norm'],
                          var_name='Platform', value_name='Score')

fig1 = px.bar(df_melted, x='Title', y='Score', color='Platform', barmode='group', 
             template='plotly_dark', title='<b>CHART 1: Cross-Platform Rating Variance</b>')

# Pop-up içeriğini özelleştiriyoruz
fig1.update_traces(
    hovertemplate="<b>Movie:</b> %{x}<br><b>Source:</b> %{customdata[0]}<br><b>Score:</b> %{y}/100<extra></extra>",
    customdata=df_melted[['Platform']],
    hoverlabel=hover_style
)

fig1.add_annotation(
    text="<b>EXPLANATION:</b> Large gaps indicate polarized reviews.",
    xref="paper", yref="paper", x=0, y=1.1, showarrow=False, align="left", font=dict(size=14, color="yellow")
)

fig1.update_layout(margin=dict(t=150, b=100), xaxis=dict(tickangle=-45))
fig1.write_html(f"{output_dir}/variance_analysis.html")


# --- CHART 2: Line Chart (Büyük Pop-up) ---
fig2 = px.line(df_scores, x='Title', y='MovieScore', markers=True, 
              template='plotly_dark', title='<b>CHART 2: Company A - Movie Score (MS) Index</b>')

# Pop-up'a "Analysis" kısmı ekledik
fig2.update_traces(
    hovertemplate="<b>%{x}</b><br><br>Final MS Score: <b>%{y:.1f}</b><br><i>Status: Analysis Completed</i><extra></extra>",
    hoverlabel=hover_style,
    marker=dict(size=12)
)

fig2.add_annotation(
    text="<b>EXPLANATION:</b> The core success metric for Company A.",
    xref="paper", yref="paper", x=0, y=1.1, showarrow=False, align="left", font=dict(size=14, color="#FFD700")
)

fig2.update_layout(margin=dict(t=150, b=100), xaxis=dict(tickangle=-45), yaxis=dict(range=[0, 115]))
fig2.write_html(f"{output_dir}/ms_index.html")


# --- CHART 3: Radar Chart (Büyük Pop-up) ---
categories = ['IMDB', 'Rotten Tomatoes', 'Metacritic']
fig3 = go.Figure()
for _, row in df_clean.iterrows():
    fig3.add_trace(go.Scatterpolar(
        r=[row['IMDB_norm'], row['RottenTomatoes_norm'], row['Metacritic_norm']],
        theta=categories, fill='toself', name=row['Title'],
        hovertemplate="<b>%{theta}</b>: %{r}/100<extra></extra>"
    ))

fig3.update_layout(
    template='plotly_dark', 
    title=dict(text="<b>CHART 3: Audience Persona Analysis</b>", y=0.95),
    margin=dict(t=180, b=50),
    hoverlabel=hover_style
)

fig3.add_annotation(
    text="<b>EXPLANATION:</b> Identifies the 'DNA' of a movie.",
    xref="paper", yref="paper", x=0.5, y=1.3, showarrow=False, align="center", font=dict(size=13, color="cyan")
)
fig3.write_html(f"{output_dir}/persona_analysis.html")

print("Yeni HTML dosyaları büyük pop-up'larla birlikte oluşturuldu.")