# Daily Journal Prompt Generator - Webapp Refactoring Plan
## Overview
Refactor the existing Python CLI application into a modern web application with FastAPI backend and a lightweight frontend. The system will maintain all existing functionality while providing a web-based interface for easier access and better user experience.
- **Configuration**: `.env` file for API keys, `settings.cfg` for app settings
- **Functionality**:
1. AI-powered prompt generation using OpenAI-compatible APIs
2. Smart repetition avoidance with 60-prompt history buffer
3. Prompt pool system for offline usage
4. Interactive CLI with rich formatting
### Key Features to Preserve
1. AI prompt generation with history awareness
2. Prompt pool management (fill, draw, stats)
3. Configuration via environment variables
4. JSON-based data persistence
5. All existing prompt generation logic
As the user discards prompts, the themes will be very slowly steered, so it's okay to take some inspiration from the history.
## Proposed Web Application Architecture
### Backend: FastAPI
**Rationale**: FastAPI provides async capabilities, automatic OpenAPI documentation, and excellent performance. It's well-suited for AI API integrations.
**Components**:
1.**API Endpoints**:
-`GET /api/prompts/draw` - Draw prompts from pool
-`POST /api/prompts/fill-pool` - Fill prompt pool using AI
-`GET /api/prompts/stats` - Get pool and history statistics
-`GET /api/prompts/history` - Get prompt history
-`POST /api/prompts/select/{prompt_id}` - Select a prompt for journaling
2.**Core Services**:
- PromptGeneratorService (adapted from existing logic)
**Webapp Option**: Basic session-based auth or JWT
**Recommendation**: Start without auth, add later if needed for multi-user
### 2. Data Storage Evolution
**Phase 1**: JSON files (maintain compatibility)
**Phase 2**: SQLite with migration script
**Phase 3**: Optional PostgreSQL for scalability
### 3. API Design Principles
- RESTful endpoints
- JSON responses
- Consistent error handling
- OpenAPI documentation
- Versioning (v1/ prefix)
### 4. Frontend State Management
**Simple approach**: React-like state with Astro components
**If complex**: Consider lightweight state management (Zustand, Jotai)
**Initial**: Component-level state sufficient
## Development Workflow
### Local Development
```bash
# Clone and setup
git clone <repo>
cd daily-journal-prompt-webapp
# Start with Docker Compose
docker-compose up --build
# Or develop separately
cd backend && uvicorn main:app --reload
cd frontend && npm run dev
```
### Testing Strategy
- **Backend**: pytest with FastAPI TestClient
- **Frontend**: Vitest for unit tests, Playwright for E2E
- **Integration**: Docker Compose test environment
### CI/CD Considerations
- GitHub Actions for testing
- Docker image building
- Deployment to cloud platform (Render, Railway, Fly.io)
## Risk Assessment & Mitigation
### Risks
1.**API Key exposure**: Use environment variables, never commit to repo
2.**Data loss during migration**: Backup JSON files, incremental migration
3.**Performance issues**: Monitor API response times, optimize database queries
4.**Browser compatibility**: Use modern CSS/JS, test on target browsers
### Mitigations
- Comprehensive testing
- Gradual rollout
- Monitoring and logging
- Regular backups
## Success Metrics
1.**Functionality**: All CLI features available in webapp
2.**Performance**: API response < 200ms, page load < 2s
3.**Usability**: Intuitive UI, mobile-responsive
4.**Reliability**: 99.9% uptime, error rate < 1%
5.**Maintainability**: Clean code, good test coverage, documented APIs
## Next Steps
### Immediate Actions
1. Create project structure with backend/frontend directories
2. Set up FastAPI backend skeleton
3. Begin refactoring core prompt generation logic
4. Create basic Astro frontend
5. Implement Docker configuration
### Future Enhancements
1. User accounts and prompt history per user
2. Prompt customization options
3. Export functionality (PDF, Markdown)
4. Mobile app (React Native)
5. Social features (share prompts, community)
## Conclusion
The refactoring from CLI to webapp will significantly improve accessibility and user experience while maintaining all existing functionality. The proposed architecture using FastAPI + Astro provides a modern, performant, and maintainable foundation for future enhancements.
The phased approach allows for incremental development with clear milestones and risk mitigation at each step.