import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# Load the dataset
df = pd.read_csv("Mock_Habit_and_RENSS_Data_for_20_Participants.csv")

# Convert date to datetime for proper ordering
df['Date'] = pd.to_datetime(df['Date'])

# 2. Wilcoxon signed rank test for day 1 vs day 7 of RENSS scale
# First, get the data for day 1 (2025-05-27) and day 7 (2025-06-02)
day1 = df[df['Date'] == '2025-05-27']
day7 = df[df['Date'] == '2025-06-02']

# Combine the participants for both days to ensure we're comparing the same users
common_participants = set(day1['Participant']).intersection(set(day7['Participant']))
print(f"Number of participants with data on both day 1 and day 7: {len(common_participants)}")

# Create a list to store results
wilcoxon_results = []

# Calculate the total RENSS score for each participant on each day
day1['RENSS_Total'] = day1.iloc[:, 4:16].sum(axis=1)  # Columns RENSS_1 to RENSS_12
day7['RENSS_Total'] = day7.iloc[:, 4:16].sum(axis=1)

# Perform Wilcoxon test for the total RENSS score
day1_total = day1.set_index('Participant')['RENSS_Total']
day7_total = day7.set_index('Participant')['RENSS_Total']

# Ensure we're comparing the same participants
day1_total = day1_total[day1_total.index.isin(common_participants)]
day7_total = day7_total[day7_total.index.isin(common_participants)]

# Sort both series by participant for proper comparison
day1_total = day1_total.sort_index()
day7_total = day7_total.sort_index()

# Perform the Wilcoxon test
w_stat, p_value = stats.wilcoxon(day1_total, day7_total)
wilcoxon_results.append({
    'Scale': 'RENSS_Total',
    'Day1_Mean': day1_total.mean(),
    'Day7_Mean': day7_total.mean(),
    'Difference': day7_total.mean() - day1_total.mean(),
    'W_statistic': w_stat,
    'P_value': p_value
})

# Also perform the test for each individual RENSS item
for i in range(1, 13):
    col = f'RENSS_{i}'
    day1_item = day1.set_index('Participant')[col]
    day7_item = day7.set_index('Participant')[col]
    
    # Ensure we're comparing the same participants
    day1_item = day1_item[day1_item.index.isin(common_participants)]
    day7_item = day7_item[day7_item.index.isin(common_participants)]
    
    # Sort both series by participant
    day1_item = day1_item.sort_index()
    day7_item = day7_item.sort_index()
    
    # Perform the Wilcoxon test
    w_stat, p_value = stats.wilcoxon(day1_item, day7_item)
    wilcoxon_results.append({
        'Scale': col,
        'Day1_Mean': day1_item.mean(),
        'Day7_Mean': day7_item.mean(),
        'Difference': day7_item.mean() - day1_item.mean(),
        'W_statistic': w_stat,
        'P_value': p_value
    })

# Convert to DataFrame for better display
wilcoxon_df = pd.DataFrame(wilcoxon_results)
print("\nWilcoxon signed rank test results (Day 1 vs Day 7):")
print(wilcoxon_df)

# Save the results to a CSV for later use
wilcoxon_df.to_csv("wilcoxon_results.csv", index=False)
print("\nWilcoxon test results saved to 'wilcoxon_results.csv'")

# 3. Create a dataframe for the relationship between habit performance and object placement
# First, calculate habit adherence rate by day and whether object was in position
habit_object_by_day = df.groupby(['Date', 'ObjectInPosition'])['HabitPerformed'].apply(
    lambda x: (x == 'Yes').mean() * 100).reset_index()
print("\nHabit completion rate by day and object position:")
print(habit_object_by_day)

# Save for later use
habit_object_by_day.to_csv("habit_object_by_day.csv", index=False)