import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/auth_provider.dart'; import '../services/api_service.dart'; import '../models/conversation.dart'; class HistoryScreen extends StatefulWidget { final Function(String) onSelectConversation; const HistoryScreen({ super.key, required this.onSelectConversation, }); @override State createState() => _HistoryScreenState(); } class _HistoryScreenState extends State { final ApiService _apiService = ApiService(); List _conversations = []; bool _isLoading = true; String? _error; @override void initState() { super.initState(); _loadConversations(); } Future _loadConversations() async { final auth = context.read(); _apiService.setToken(auth.token); setState(() { _isLoading = true; _error = null; }); try { final conversations = await _apiService.getConversations(); setState(() { _conversations = conversations; _isLoading = false; }); } catch (e) { setState(() { _error = e.toString(); _isLoading = false; }); } } Future _deleteConversation(String id) async { try { await _apiService.deleteConversation(id); setState(() { _conversations.removeWhere((c) => c.id == id); }); } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Failed to delete: $e'), backgroundColor: Colors.red.shade700, ), ); } } } IconData _getTitleIcon(String title) { final lower = title.toLowerCase(); if (lower.contains('error') || lower.contains('fix') || lower.contains('bug')) { return Icons.bug_report; } if (lower.contains('test')) { return Icons.science; } if (lower.contains('backup') || lower.contains('restore')) { return Icons.backup; } if (lower.contains('deploy') || lower.contains('build')) { return Icons.rocket_launch; } if (lower.contains('install') || lower.contains('setup')) { return Icons.download; } return Icons.chat_bubble_outline; } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFF1A1A1A), appBar: AppBar( backgroundColor: const Color(0xFF2D2D2D), title: const Text('Conversations'), actions: [ IconButton( icon: const Icon(Icons.refresh), onPressed: _loadConversations, ), ], ), body: _buildBody(), ); } Widget _buildBody() { if (_isLoading) { return const Center( child: CircularProgressIndicator(color: Colors.orange), ); } if (_error != null) { return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.error_outline, size: 48, color: Colors.red.shade400), const SizedBox(height: 16), Text( _error!, style: TextStyle(color: Colors.grey.shade400), textAlign: TextAlign.center, ), const SizedBox(height: 16), ElevatedButton( onPressed: _loadConversations, style: ElevatedButton.styleFrom( backgroundColor: Colors.orange.shade700, ), child: const Text('Retry'), ), ], ), ); } if (_conversations.isEmpty) { return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.chat_bubble_outline, size: 64, color: Colors.grey.shade700), const SizedBox(height: 16), Text( 'No conversations yet', style: TextStyle( color: Colors.grey.shade400, fontSize: 18, ), ), const SizedBox(height: 8), Text( 'Start a new chat to get started', style: TextStyle( color: Colors.grey.shade600, fontSize: 14, ), ), ], ), ); } return RefreshIndicator( onRefresh: _loadConversations, color: Colors.orange, child: ListView.builder( padding: const EdgeInsets.symmetric(vertical: 8), itemCount: _conversations.length, itemBuilder: (context, index) { final conv = _conversations[index]; return Dismissible( key: Key(conv.id), direction: DismissDirection.endToStart, background: Container( alignment: Alignment.centerRight, padding: const EdgeInsets.only(right: 20), color: Colors.red.shade700, child: const Icon(Icons.delete, color: Colors.white), ), confirmDismiss: (_) async { return await showDialog( context: context, builder: (context) => AlertDialog( backgroundColor: const Color(0xFF2D2D2D), title: const Text( 'Delete Conversation?', style: TextStyle(color: Colors.white), ), content: const Text( 'This action cannot be undone.', style: TextStyle(color: Colors.white70), ), actions: [ TextButton( onPressed: () => Navigator.pop(context, false), child: const Text('Cancel'), ), TextButton( onPressed: () => Navigator.pop(context, true), style: TextButton.styleFrom( foregroundColor: Colors.red.shade400, ), child: const Text('Delete'), ), ], ), ); }, onDismissed: (_) => _deleteConversation(conv.id), child: ListTile( leading: CircleAvatar( backgroundColor: const Color(0xFF3D3D3D), child: Icon( _getTitleIcon(conv.displayTitle), color: Colors.orange.shade400, size: 20, ), ), title: Text( conv.displayTitle, style: const TextStyle( color: Colors.white, fontWeight: FontWeight.w500, ), maxLines: 1, overflow: TextOverflow.ellipsis, ), subtitle: Text( '${conv.timeAgo} • ${conv.messageCount} messages', style: TextStyle( color: Colors.grey.shade500, fontSize: 12, ), ), trailing: Icon( Icons.chevron_right, color: Colors.grey.shade600, ), onTap: () { widget.onSelectConversation(conv.id); Navigator.pop(context); }, ), ); }, ), ); } }