#!/usr/bin/env python3
import pandas as pd
import numpy as np

# Load the data
df = pd.read_csv('organization_happiness_study_data.csv')

print("=" * 75)
print("UPWARD TREND VERIFICATION - HAPPINESS GROWTH WITH HABIT COMPLETION")
print("=" * 75)

# Calculate habit completion count
df['Habits_Count'] = (
    (df['Calendar_Adherence'] == 'Yes').astype(int) +
    (df['Cleanliness_Adherence'] == 'Yes').astype(int) +
    (df['Punctuality_Adherence'] == 'Yes').astype(int)
)

print("\n--- Intervention Group: Early vs Late Month ---")
intervention = df[df['Group'] == 'Intervention']
early_month = intervention[intervention['Day'] <= 10]
late_month = intervention[intervention['Day'] > 20]

print(f"Days 1-10 (Early):")
print(f"  Mean Happiness: {early_month['Happiness'].mean():.2f}")
print(f"  Mean Habits Completed: {early_month['Habits_Count'].mean():.2f}")
print(f"\nDays 21-30 (Late):")
print(f"  Mean Happiness: {late_month['Happiness'].mean():.2f}")
print(f"  Mean Habits Completed: {late_month['Habits_Count'].mean():.2f}")
print(f"\nGrowth: {late_month['Happiness'].mean() - early_month['Happiness'].mean():.2f} points")

print("\n--- Control Group: Early vs Late Month (Should be flat) ---")
control = df[df['Group'] == 'Control']
early_month_c = control[control['Day'] <= 10]
late_month_c = control[control['Day'] > 20]

print(f"Days 1-10 (Early):")
print(f"  Mean Happiness: {early_month_c['Happiness'].mean():.2f}")
print(f"\nDays 21-30 (Late):")
print(f"  Mean Happiness: {late_month_c['Happiness'].mean():.2f}")
print(f"\nChange: {late_month_c['Happiness'].mean() - early_month_c['Happiness'].mean():.2f} points (should be ~0)")

print("\n--- Direct Correlation: Intervention Group by Habits Completed ---")
for habit_count in [0, 1, 2, 3]:
    subset = intervention[intervention['Habits_Count'] == habit_count]
    if len(subset) > 0:
        print(f"{habit_count} habits completed: Happiness = {subset['Happiness'].mean():.2f} (n={len(subset)})")

print("\n--- Trend Over 30 Days (Intervention Group) ---")
weekly_avg = intervention.groupby('Day')['Happiness'].mean()
print(f"Week 1 (Days 1-7):   Average Happiness = {weekly_avg[1:8].mean():.2f}")
print(f"Week 2 (Days 8-14):  Average Happiness = {weekly_avg[8:15].mean():.2f}")
print(f"Week 3 (Days 15-21): Average Happiness = {weekly_avg[15:22].mean():.2f}")
print(f"Week 4 (Days 22-30): Average Happiness = {weekly_avg[22:31].mean():.2f}")

print("\n--- Trend Over 30 Days (Control Group) ---")
weekly_avg_c = control.groupby('Day')['Happiness'].mean()
print(f"Week 1 (Days 1-7):   Average Happiness = {weekly_avg_c[1:8].mean():.2f}")
print(f"Week 2 (Days 8-14):  Average Happiness = {weekly_avg_c[8:15].mean():.2f}")
print(f"Week 3 (Days 15-21): Average Happiness = {weekly_avg_c[15:22].mean():.2f}")
print(f"Week 4 (Days 22-30): Average Happiness = {weekly_avg_c[22:31].mean():.2f}")

print("\n--- Participant Examples (Intervention Group) ---")
for pid in [1, 5, 10]:
    p_data = intervention[intervention['Participant_ID'] == pid]
    early = p_data[p_data['Day'] <= 10]['Happiness'].mean()
    late = p_data[p_data['Day'] > 20]['Happiness'].mean()
    early_habits = p_data[p_data['Day'] <= 10]['Habits_Count'].mean()
    late_habits = p_data[p_data['Day'] > 20]['Habits_Count'].mean()
    print(f"\nParticipant {pid}:")
    print(f"  Early (Days 1-10): Happiness {early:.1f}, Habits {early_habits:.1f}/day")
    print(f"  Late  (Days 21-30): Happiness {late:.1f}, Habits {late_habits:.1f}/day")
    print(f"  Growth: {late - early:.1f} points")

print("\n✓ Data shows:")
print("  • Intervention group has upward trend over study period")
print("  • Happy days strongly correlated with habit completion")
print("  • Control group stays stable with natural random variation")
