"""
Coach AI capstone — original Coach AI notebook (Google Colab, May 2026)
Code cells only, exported unchanged for the portfolio. Outputs removed.
Cell 3 is the first draft of coach_ai(); cell 4 is the corrected version that was run.
"""

# %% [cell 0]
import pandas as pd

# %% [cell 1]
url = "/content/CIS 627 Athlete Performance Capstone - recovery_data (1).csv"

df = pd.read_csv(url)

# %% [cell 2]
# Clean column names
df.columns = df.columns.str.lower().str.strip()

# Rename to match code
df = df.rename(columns={
    'resting hr': 'resting_hr',
    'sorness': 'soreness'
})

# Preview
df.head()

# %% [cell 3]
def coach_ai(row):
    sleep = row['sleep']
    hrv = row['hrv']
    resting_hr = row['resting_hr']
    soreness = row['soreness']
    fatigue = row['fatigue']
    hydration = row['hydration']

    # Decision logic
    if fatigue >= 4 or soreness >= 4:
        return "Recovery"

    elif hrv >= 60 and resting_hr <= 45 and fatigue <= 2:
        return "Push"

    elif sleep < 6:
        return "Light"

    else:
        return "Maintain"

# %% [cell 4]
def coach_ai(row):
    sleep = row['sleep_hours'] # Corrected from 'sleep'
    hrv = row['hrv']
    resting_hr = row['resting_hr']
    soreness = row['sorness'] # Corrected from 'soreness' to 'sorness'
    fatigue = row['fatigue']
    hydration = row['hydration']

    # Decision logic
    if fatigue >= 4 or soreness >= 4:
        return "Recovery"

    elif hrv >= 60 and resting_hr <= 45 and fatigue <= 2:
        return "Push"

    elif sleep < 6:
        return "Light"

    else:
        return "Maintain"

df['recommendation'] = df.apply(coach_ai, axis=1)

df[['sleep_hours','hrv','fatigue','sorness','recommendation']].head(15)

# %% [cell 5]
# 3-day rolling fatigue + HRV
df['fatigue_avg_3'] = df['fatigue'].rolling(3).mean()
df['hrv_avg_3'] = df['hrv'].rolling(3).mean()

# %% [cell 6]
def coach_ai_v2(row):
    sleep = row['sleep_hours'] # Corrected from 'sleep'
    hrv = row['hrv']
    resting_hr = row['resting_hr']
    soreness = row['sorness'] # Corrected from 'soreness'
    fatigue = row['fatigue']
    hydration = row['hydration']
    fatigue_avg = row['fatigue_avg_3']
    hrv_avg = row['hrv_avg_3']

    if fatigue_avg >= 4:
        return "Recovery (Trend)"

    elif hrv_avg >= 60 and fatigue <= 2:
        return "Push (Recovered)"

    elif sleep < 6:
        return "Light (Low Sleep)"

    else:
        return "Maintain"

# %% [cell 7]
# 3-day rolling fatigue + HRV
df['fatigue_avg_3'] = df['fatigue'].rolling(3).mean()
df['hrv_avg_3'] = df['hrv'].rolling(3).mean()

df['recommendation_v2'] = df.apply(coach_ai_v2, axis=1)

df[['sleep_hours','fatigue','fatigue_avg_3','recommendation_v2']].tail(15)

# %% [cell 8]
df.to_csv("coach_ai_results.csv", index=False)

# %% [cell 9]
df[['sleep_hours','hrv','resting_hr','sorness','fatigue','hydration','recommendation_v2']]

# %% [cell 10]
print("Total Days:", len(df))
print("Average Sleep:", df['sleep_hours'].mean())
print("Average HRV:", df['hrv'].mean())
print("Average Resting HR:", df['resting_hr'].mean())
print("Average Fatigue:", df['fatigue'].mean())
print("Average Soreness:", df['sorness'].mean())

# %% [cell 11]
print(df['recommendation_v2'].value_counts())

# %% [cell 12]
bad_days = df[(df['fatigue'] >= 4) | (df['sorness'] >= 4)]

print("High Stress Days:")
print(bad_days[['sleep_hours','fatigue','sorness']])

# %% [cell 13]
if df['hrv'].mean() > 60:
    print("Overall Status: Well Recovered")
elif df['fatigue'].mean() >= 3:
    print("Overall Status: Moderate Fatigue")
else:
    print("Overall Status: Balanced Training")

# %% [cell 14]
import matplotlib.pyplot as plt

plt.figure()
plt.plot(df['hrv'], label='HRV')
plt.plot(df['fatigue'], label='Fatigue')
plt.legend()
plt.title("HRV vs Fatigue Over Time")
plt.show()
