"""
ACT 4: THE AUTHOR - The Sandpit (Three Contradictory Stories)
Dataset: workforce_dynamics.csv
Purpose: Same data, three different narratives. The Author chooses the story.
"""

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

# ── Load Data ──────────────────────────────────────────────
df = pd.read_csv("data/workforce_dynamics.csv")

# ══════════════════════════════════════════════════════════════
# STORY 1: THE RTO ADVOCATE
# Headline: "Office Workers Get Promoted — Remote Workers Don't. Period."
# Cherry-pick: promotion rate by work setup (100 employees)
# ══════════════════════════════════════════════════════════════

promo = df.groupby("Work_Setup").agg(
    total=("Promoted_Last_Year", "count"),
    promoted=("Promoted_Last_Year", lambda x: (x == "Yes").sum())
).reset_index()
promo["Promotion_Rate"] = (promo["promoted"] / promo["total"] * 100).round(1)

# Order for narrative impact
order = ["On-Site", "Hybrid", "Remote"]
promo["Work_Setup"] = pd.Categorical(promo["Work_Setup"], categories=order, ordered=True)
promo = promo.sort_values("Work_Setup")

colors_rto = ["#e31937", "#f97316", "#334155"]

fig_rto = go.Figure(go.Bar(
    x=promo["Work_Setup"],
    y=promo["Promotion_Rate"],
    marker_color=colors_rto,
    text=[f"{v}%" for v in promo["Promotion_Rate"]],
    textposition="outside",
    textfont=dict(size=16, color="#f1f5f9"),
))
fig_rto.update_layout(
    title="📢 RTO Advocate: On-Site = 67%, Remote = 0%. Case Closed?",
    template="plotly_dark",
    paper_bgcolor="#0f172a", plot_bgcolor="#0f172a",
    yaxis_title="Promotion Rate (%)",
    title_font_size=16,
    yaxis=dict(range=[0, 80]),
)
fig_rto.show()

# ══════════════════════════════════════════════════════════════
# STORY 2: THE UNION REP
# Headline: "High Performers Are Burning Out"
# Cherry-pick: hours vs mental health for top performers (11 in burnout zone)
# ══════════════════════════════════════════════════════════════

fig_union = px.scatter(
    df,
    x="Weekly_Hours",
    y="Mental_Health_Index",
    color="Productivity_Score",
    size="Salary",
    hover_name="Emp_ID",
    title="⚠️ Union Rep: High Performers Are Burning Out",
    labels={
        "Weekly_Hours": "Weekly Hours Worked",
        "Mental_Health_Index": "Mental Health Index (1-10)",
        "Productivity_Score": "Productivity",
        "Salary": "Salary ($)",
    },
    color_continuous_scale="RdYlGn",
    template="plotly_dark",
)
fig_union.update_layout(
    paper_bgcolor="#0f172a", plot_bgcolor="#0f172a",
    title_font_size=16,
)
# Add a danger zone annotation (max hours = 60 in new data)
fig_union.add_shape(
    type="rect", x0=50, x1=62, y0=0, y1=5,
    fillcolor="rgba(239,68,68,0.1)", line=dict(color="#ef4444", dash="dash"),
)
fig_union.add_annotation(
    x=56, y=2.5, text="BURNOUT ZONE\n(11 employees, all 85+ productivity)",
    font=dict(color="#ef4444", size=12), showarrow=False,
)
fig_union.show()

# ══════════════════════════════════════════════════════════════
# STORY 3: THE DEI AUDITOR
# Headline: "Gender Pay Gaps Persist Across Departments"
# Cherry-pick: salary by gender and department (100 employees)
# ══════════════════════════════════════════════════════════════

pay = df.groupby(["Department", "Gender"])["Salary"].mean().reset_index()

fig_dei = px.bar(
    pay,
    x="Department",
    y="Salary",
    color="Gender",
    barmode="group",
    title="🔍 DEI Auditor: Gender Pay Gaps Persist Across Departments",
    labels={"Salary": "Average Salary ($)", "Department": ""},
    color_discrete_map={
        "Male": "#3b82f6",
        "Female": "#e31937",
        "Non-Binary": "#a855f7",
    },
    template="plotly_dark",
)
fig_dei.update_layout(
    paper_bgcolor="#0f172a", plot_bgcolor="#0f172a",
    title_font_size=16,
    legend=dict(orientation="h", y=-0.15),
)
fig_dei.show()

# ══════════════════════════════════════════════════════════════
# BONUS: All Three Perspectives Summary Table
# ══════════════════════════════════════════════════════════════

print("""
THE AUTHOR'S POWER (100-employee dataset):
──────────────────────────────────────────
Same dataset. Three completely different — and arguably truthful — stories.

📢 RTO ADVOCATE:  "On-site = 66.7% promotion rate. Hybrid = 37.9%. Remote = 0%."
   → Filters: Promotion by Work_Setup only. Ignores tenure, hours, department,
     and the fact that on-site workers average 50+ hours with high visibility.

⚠️ UNION REP:     "11 employees in the burnout zone (50+ hrs, MH < 5) — ALL high performers."
   → Filters: Hours vs Mental Health. Ignores that many 40-hour workers
     report great mental health. Selection bias in who "burns out."

🔍 DEI AUDITOR:   "Sales: Males $121K vs Females $94K. Engineering: Males $141K vs Females $123K."
   → Filters: Salary by Gender by Department. Ignores tenure/seniority differences
     and that sample sizes vary widely (e.g., Non-Binary n=1 in Marketing).

THE LESSON: None of these are LIES. But none tell the WHOLE truth.
A great Data Professional presents the nuance, not just the narrative.
""")
