"""
ACT 2: THE SCIENTIST - Correlation ≠ Causation
Dataset: tech_hiring_macro_trends.csv
Purpose: Teach nuance — data-driven narratives are not black and white.
"""

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

# ── Load Data ──────────────────────────────────────────────
df = pd.read_csv("data/tech_hiring_macro_trends.csv", parse_dates=["Date"])

# ══════════════════════════════════════════════════════════════
# SCATTER 1: Dev Jobs vs. ChatGPT Traffic
# Shows strong negative correlation — but is it causal?
# ══════════════════════════════════════════════════════════════

# Filter to only rows where ChatGPT exists
df_chat = df[df["ChatGPT_Traffic"] > 0].copy()

fig1 = px.scatter(
    df_chat,
    x="ChatGPT_Traffic",
    y="Dev_Jobs",
    trendline="ols",
    title="Dev Jobs vs. ChatGPT Traffic — Looks Scary, Right?",
    labels={
        "ChatGPT_Traffic": "ChatGPT Search Interest",
        "Dev_Jobs": "Dev Job Postings (Index)"
    },
    color_discrete_sequence=["#ef4444"],
    template="plotly_dark",
)
fig1.update_layout(
    paper_bgcolor="#0f172a", plot_bgcolor="#0f172a",
    title_font_size=16,
)
fig1.show()

# ══════════════════════════════════════════════════════════════
# SCATTER 2: Dev Jobs vs. Federal Interest Rate
# Also strong negative correlation — and economically logical.
# ══════════════════════════════════════════════════════════════

fig2 = px.scatter(
    df,
    x="Fed_Interest",
    y="Dev_Jobs",
    trendline="ols",
    title="Dev Jobs vs. Federal Interest Rate — The Real Driver",
    labels={
        "Fed_Interest": "Federal Interest Rate (%)",
        "Dev_Jobs": "Dev Job Postings (Index)"
    },
    color_discrete_sequence=["#3b82f6"],
    template="plotly_dark",
)
fig2.update_layout(
    paper_bgcolor="#0f172a", plot_bgcolor="#0f172a",
    title_font_size=16,
)
fig2.show()

# ══════════════════════════════════════════════════════════════
# SCATTER 3: AI Jobs vs. Dev Jobs (inverted trend)
# While dev jobs drop, AI-specific roles are GROWING.
# ══════════════════════════════════════════════════════════════

fig3 = px.scatter(
    df,
    x="AI_Jobs",
    y="Dev_Jobs",
    trendline="ols",
    title="The Pivot: Dev Jobs Shrink as AI Jobs Grow",
    labels={
        "AI_Jobs": "AI Job Postings (Index)",
        "Dev_Jobs": "Dev Job Postings (Index)"
    },
    color_discrete_sequence=["#a855f7"],
    template="plotly_dark",
)
fig3.update_layout(
    paper_bgcolor="#0f172a", plot_bgcolor="#0f172a",
    title_font_size=16,
)
fig3.show()

# ══════════════════════════════════════════════════════════════
# CORRELATION MATRIX (Advanced)
# Show all pairwise correlations in the dataset.
# ══════════════════════════════════════════════════════════════

corr = df[["Dev_Jobs", "ChatGPT_Traffic", "Fed_Interest", "AI_Jobs"]].corr()

fig4 = go.Figure(data=go.Heatmap(
    z=corr.values,
    x=corr.columns,
    y=corr.columns,
    colorscale="RdBu",
    zmid=0,
    text=corr.values.round(2),
    texttemplate="%{text}",
    textfont=dict(size=14),
))
fig4.update_layout(
    title="Correlation Matrix — Everything Correlates, But Why?",
    template="plotly_dark",
    paper_bgcolor="#0f172a",
    plot_bgcolor="#0f172a",
    title_font_size=16,
    width=600, height=500,
)
fig4.show()

print("""
KEY TAKEAWAY:
─────────────
ChatGPT_Traffic vs Dev_Jobs:  strong negative correlation.
Fed_Interest vs Dev_Jobs:     strong negative correlation.
AI_Jobs vs Dev_Jobs:          strong negative correlation.

ALL three correlate — but only ONE is a causal economic driver.
The Scientist asks: "What mechanism connects these variables?"

Interest rates ↑ → Venture capital dries up → Hiring freezes.
ChatGPT launched at the SAME TIME — coincidence, not causation.
""")
