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

# Verileri oku
df_clean = pd.read_csv('data/processed/movies_clean.csv')
df_scores = pd.read_csv('data/processed/movie_scores.csv')

# Ortak Stil Ayarları
COMMON_STYLE = dict(
    xref="paper", yref="paper",
    x=0.02, y=0.98, # Sol üst köşeye sabitledik
    showarrow=False,
    align="left",
    bgcolor="rgba(0, 0, 0, 0.7)", # Arka planı biraz daha koyu yaptık ki yazı netleşsin
    bordercolor="gray",
    borderwidth=1,
    font=dict(size=12, color="white"),
    width=350 # Genişliği daralttık ki grafiği kapatmasın
)

# --- CHART 1: Bar Chart ---
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',
             title='<b>CHART 1: Cross-Platform Rating Variance</b>',
             barmode='group', template='plotly_dark')

fig1.add_annotation(
    **COMMON_STYLE,
    text=(
        "<b>HOW TO READ:</b><br>"
        "• Compares audience vs. critic reactions.<br>"
        "• Large gaps indicate 'Polarized' movies.<br>"
        "• <i>Goal:</i> Identify if a movie is a fan-favorite<br>"
        "  or a critical masterpiece."
    )
)
fig1.update_layout(margin=dict(t=100, b=120), xaxis=dict(tickangle=-45))
fig1.show()

# --- CHART 2: Line Chart ---
top_movie = df_scores.loc[df_scores['MovieScore'].idxmax()]

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

fig2.add_annotation(
    **COMMON_STYLE,
    text=(
        "<b>HOW TO READ:</b><br>"
        "• The core metric for Company A.<br>"
        "• Upward trend = Improving portfolio quality.<br>"
        "• <b>The Peak:</b> Best overall performance."
    )
)
fig2.update_layout(margin=dict(t=100, b=120), xaxis=dict(tickangle=-45), yaxis=dict(range=[0, 115]))
fig2.show()

# --- CHART 3: Radar Chart ---
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']
    ))

fig3.add_annotation(
    xref="paper", yref="paper",
    x=0, y=1.15, # Radar için başlığın hemen altına aldık
    text=(
        "<b>HOW TO READ:</b><br>"
        "• <b>IMDB Side:</b> Popular appeal.<br>"
        "• <b>Metacritic Side:</b> Critical acclaim.<br>"
        "• <b>Prediction:</b> Targets future gaps."
    ),
    showarrow=False, align="left", bgcolor="rgba(0, 0, 0, 0.7)",
    bordercolor="cyan", borderwidth=1, font=dict(size=11)
)

fig3.update_layout(
    template='plotly_dark',
    margin=dict(t=150, b=50), # Üst marjini açıklamaya yer açmak için artırdık
    polar=dict(radialaxis=dict(visible=True, range=[0, 100])),
    legend=dict(orientation="h", y=-0.2)
)
fig3.show()