# Tasks: Backup Scheduler & Unified Task UI

## Phase 1: Setup
- [x] T001 Initialize SQLite database `tasks.db` and SQLAlchemy engine in `backend/src/core/database.py`
- [x] T002 Create SQLAlchemy model for `TaskRecord` in `backend/src/models/task.py`
- [x] T003 Update `backend/src/core/config_models.py` to include `Schedule` and update `Environment` model
- [x] T004 Create database migrations or initialization script for `tasks.db`

## Phase 2: Foundational
- [x] T005 [P] Implement `TaskPersistence` layer in `backend/src/core/task_manager/persistence.py`
- [x] T006 Update `TaskManager` in `backend/src/core/task_manager/manager.py` to use persistence for all jobs
- [x] T007 Implement `SchedulerService` using `APScheduler` in `backend/src/core/scheduler.py`
- [x] T008 Integrate `SchedulerService` into main FastAPI application startup in `backend/src/app.py`

## Phase 3: [US1] Scheduled Backups
- [x] T009 [US1] Implement schedule loading and registration logic in `SchedulerService`
- [x] T010 [US1] Update `Environment` settings API to handle `backup_schedule` updates in `backend/src/api/routes/environments.py`
- [ ] T011 [P] [US1] Add schedule configuration fields to Environment edit form in `frontend/src/components/EnvSelector.svelte` (or appropriate component)
- [x] T012 [US1] Implement validation for Cron expressions in backend and frontend

## Phase 4: [US2] Unified Task Management UI
- [x] T013 [US2] Implement `/api/tasks` endpoint to list and filter tasks in `backend/src/api/routes/tasks.py`
- [x] T014 [US2] Create new Tasks page in `frontend/src/routes/tasks/+page.svelte`
- [x] T015 [P] [US2] Implement `TaskList` component in `frontend/src/components/TaskList.svelte`
- [x] T016 [US2] Add "Tasks" link to main navigation in `frontend/src/components/Navbar.svelte`

## Phase 5: [US3] Manual Backup Trigger
- [x] T017 [US3] Implement `/api/tasks/backup` POST endpoint in `backend/src/api/routes/tasks.py`
- [x] T018 [US3] Add "Run Backup" button and environment selection to Tasks page in `frontend/src/routes/tasks/+page.svelte`

## Phase 6: [US4] Task History & Logs
- [x] T019 [US4] Implement `/api/tasks/{task_id}` GET endpoint for detailed task info and logs in `backend/src/api/routes/tasks.py`
- [x] T020 [US4] Implement `TaskLogViewer` component in `frontend/src/components/TaskLogViewer.svelte`
- [x] T021 [US4] Integrate log viewer into TaskList or as a separate modal/page

## Final Phase: Polish & Cross-cutting concerns
- [ ] T022 Implement task cleanup/retention policy (e.g., delete tasks older than 30 days)
- [x] T023 Add real-time updates for task status using WebSockets (optional/refinement)
- [x] T024 Ensure consistent error handling and logging across scheduler and task manager

## Dependencies
- US1 depends on Phase 1 & 2
- US2 depends on Phase 1 & 2
- US3 depends on US2
- US4 depends on US2

## Implementation Strategy
1. **Infrastructure First**: Setup database and basic task persistence.
2. **Backend Logic**: Implement scheduler and update task manager.
3. **API & UI**: Build the unified tasks view.
4. **Feature Integration**: Add scheduling UI and manual triggers.