# Investigation Plan: Bedtime Graph Issue ## Objective Investigate why the "Bedtime" graph shows unexpected times when a specific `bedtime_ref` is set. ## Current Understanding - The `BedtimePlot.tsx` component displays a graph where the Y-axis (or values) are formatted using `formatMinutesAfter(value, bedtime_ref)`. - The data comes from the `/api/stats/wake-sleep-times` endpoint. - The backend calculates `bedtime_minutes` based on sessions that started on or before the `bedtime_ref`. ## Investigation Steps ### Step 1: Analyze Backend Logic - [ ] Examine `get_wake_sleep_times` in `backend/app/routers/stats.py` to understand how `bedtime_minutes` is calculated. - [ ] Verify if the logic correctly identifies "bedtime" for various sleep session scenarios (e.g., sessions crossing midnight). - [ ] Identify potential edge cases where the `s.start_time.time() <= b_ref_time` condition might be problematic. ### Step 2: Analyze Frontend Logic - [ ] Examine `BedtimePlot.tsx` and `useStats.ts` to see how the data is consumed and formatted. - [ ] Understand how `formatMinutesAfter` interacts with the values returned by the backend. ### Step 3: Data Simulation & Verification - [ ] Create a set of test cases (mocked data) representing different sleep scenarios. - [ ] Manually calculate the expected `bedtime_minutes` for these scenarios using the current logic. - [ ] Compare with what the user reported to confirm the bug's nature. ### Step 4: Propose and Implement Fixes - [ ] Refine the backend logic to better represent "bedtime" if current logic is insufficient. - [ ] Ensure the frontend correctly displays the intended time. - [ ] Verify that changes do not break other parts of the stats page (e.g., Wakeup times). ## Future Tasks - [ ] Run tests to ensure correctness after implementation. - [ ] Finalize documentation/explanation of the fix.