# Schedule System - Task Breakdown

## Pre-requisites

- [ ] Section 01 Database Schema completed
- [ ] Existing WhenIWork integration working
- [ ] Understanding of existing EmployeesController.php

## Tasks

### Task 1: Create Abstract ScheduleProvider

**Estimated Complexity**: Low
**Files**: `userfrosting/models/Class/Daybook/ScheduleProvider.php`

**Acceptance Criteria**:
- [ ] Abstract methods defined for schedule operations
- [ ] Cache helper methods implemented
- [ ] Provider pattern allows easy extension

---

### Task 2: Create WhenIWorkSchedule Provider

**Estimated Complexity**: High
**Files**: `userfrosting/models/Class/Daybook/WhenIWorkSchedule.php`

**Acceptance Criteria**:
- [ ] `isEnabled()` checks store WiW configuration
- [ ] `getScheduleForDate()` returns formatted shifts
- [ ] `getEmployeeShift()` returns single employee's shift
- [ ] `syncScheduleToCache()` stores in database
- [ ] Employee lookup matches WiW ID to local ID
- [ ] Shifts sorted by start time
- [ ] Error handling for API failures
- [ ] Tests pass

---

### Task 3: Create HomebaseSchedule Stub

**Estimated Complexity**: Low
**Files**: `userfrosting/models/Class/Daybook/HomebaseSchedule.php`

**Description**:
Create placeholder implementation for future Homebase integration.

**Acceptance Criteria**:
- [ ] Class extends ScheduleProvider
- [ ] `isEnabled()` checks for Homebase config
- [ ] Methods return empty arrays (stub)
- [ ] TODO comments for future implementation

---

### Task 4: Create ScheduleManager

**Estimated Complexity**: Medium
**Files**: `userfrosting/models/Class/Daybook/ScheduleManager.php`

**Acceptance Criteria**:
- [ ] `resolveProvider()` selects correct provider
- [ ] `getTodaySchedule()` returns today's data
- [ ] `getScheduleForDate()` returns specific date
- [ ] `refreshSchedule()` clears cache and re-fetches
- [ ] Clocked-in status enrichment works
- [ ] Tests pass

---

### Task 5: Create ScheduleApiController

**Estimated Complexity**: Medium
**Files**: `userfrosting/controllers/Daybook/ScheduleApiController.php`

**Acceptance Criteria**:
- [ ] All endpoints implemented
- [ ] Date validation on date parameter
- [ ] Ably event on refresh
- [ ] Tests pass

---

### Task 6: Create Routes

**Estimated Complexity**: Low
**Files**: `userfrosting/routes/daybook/schedule.php`

**Acceptance Criteria**:
- [ ] All endpoints defined
- [ ] Routes included in bootstrap

---

### Task 7: Create Frontend Schedule Display

**Estimated Complexity**: Medium
**Files**: `public_html/js/workspace/modules/daybook/schedule-display.js`

**Acceptance Criteria**:
- [ ] `loadSchedule()` fetches today's schedule
- [ ] `renderSchedule()` displays employee list with times
- [ ] Clocked-in indicator (green dot)
- [ ] Date navigation (prev/next day)
- [ ] `refresh()` reloads data
- [ ] `handleRemoteUpdate()` for Ably messages
- [ ] Tests pass

---

### Task 8: Create Schedule Panel Template

**Estimated Complexity**: Medium
**Files**: `userfrosting/templates/themes/default/daybook/partials/schedule-panel.html`

**Acceptance Criteria**:
- [ ] Date header with navigation
- [ ] Employee list with shift times
- [ ] Clocked-in indicator
- [ ] Position display (if available)
- [ ] "No schedule" message when disabled/empty
- [ ] Responsive layout

---

### Task 9: Create Schedule Sync Cron Script

**Estimated Complexity**: Low
**Files**: `userfrosting/scripts/daybook-schedule-sync.php`

**Acceptance Criteria**:
- [ ] Iterates all stores with scheduling enabled
- [ ] Syncs today's schedule for each
- [ ] Logs successes and failures
- [ ] Can be run via cron

---

### Task 10: Extend EmployeesController

**Estimated Complexity**: Low
**Files**: `userfrosting/controllers/WhenIWork/EmployeesController.php`

**Description**:
Add method to get clocked-in employee IDs efficiently.

**Acceptance Criteria**:
- [ ] `getClockedInEmployeeIds()` returns array of IDs
- [ ] Uses existing clocked-in detection logic
- [ ] Cached appropriately
- [ ] Tests pass

---

## Testing Tasks

### Test 1: WhenIWorkSchedule Unit Tests

**File**: `userfrosting/tests/Daybook/Unit/WhenIWorkScheduleTest.php`

**Test Cases**:
- [ ] `test_isEnabled_returns_true_when_configured`
- [ ] `test_isEnabled_returns_false_when_not_configured`
- [ ] `test_formatShifts_maps_employee_ids`
- [ ] `test_formatShifts_sorts_by_start_time`

---

### Test 2: ScheduleManager Unit Tests

**File**: `userfrosting/tests/Daybook/Unit/ScheduleManagerTest.php`

**Test Cases**:
- [ ] `test_resolveProvider_returns_wheniwork`
- [ ] `test_resolveProvider_returns_null_when_none_enabled`
- [ ] `test_enrichWithClockStatus_adds_flag`

---

### Test 3: ScheduleApiController Integration Tests

**File**: `userfrosting/tests/Daybook/Integration/ScheduleApiControllerTest.php`

**Test Cases**:
- [ ] `test_getTodaySchedule_returns_schedule`
- [ ] `test_getScheduleForDate_validates_date_format`
- [ ] `test_refreshSchedule_clears_cache`

---

## Completion Checklist

- [ ] Abstract provider created
- [ ] WhenIWork provider created
- [ ] Homebase stub created
- [ ] Manager created
- [ ] API controller created
- [ ] Routes defined
- [ ] Frontend JavaScript created
- [ ] Templates created
- [ ] Cron script created
- [ ] EmployeesController extended
- [ ] All tests passing
