import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import '../../core/auth/auth_bloc.dart'; import '../../core/api/api_client.dart'; import '../../core/pairing/pairing_bloc.dart'; import '../chat/widgets/pricing_bottom_sheet.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State with WidgetsBindingObserver { Map? _activeSession; bool _loadingSession = true; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); _checkActiveSession(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { _checkActiveSession(); } } @override void didChangeDependencies() { super.didChangeDependencies(); // Re-check when navigating back to this screen _checkActiveSession(); } Future _checkActiveSession() async { try { final apiClient = context.read(); final response = await apiClient.get('/api/client/chat/session/active'); final data = response['data']; if (mounted) { setState(() { _activeSession = data is Map ? data : null; _loadingSession = false; }); } } catch (_) { if (mounted) setState(() => _loadingSession = false); } } @override Widget build(BuildContext context) { return BlocListener( listener: (context, state) { if (state is PairingSearching) { context.go('/chat/searching'); } else if (state is PairingNoBestie) { context.go('/chat/no-bestie'); } else if (state is PairingError) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(state.message)), ); } }, child: BlocBuilder( builder: (context, state) { final displayName = state is AuthAuthenticated ? state.profile['display_name'] as String : state is AuthAnonymous ? state.displayName : ''; return Scaffold( appBar: AppBar( title: const Text('Halo Bestie'), actions: [ IconButton( icon: const Icon(Icons.history), onPressed: () => context.push('/chat/history'), ), IconButton( icon: const Icon(Icons.logout), onPressed: () => context.read().add(LogoutRequested()), ), ], ), body: Center( child: Padding( padding: const EdgeInsets.all(32), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Halo, $displayName!', style: const TextStyle(fontSize: 24)), const SizedBox(height: 32), if (_loadingSession) const CircularProgressIndicator() else if (_activeSession != null) _ActiveSessionCard( session: _activeSession!, onTap: () { final sessionId = _activeSession!['id'] as String; final mitraName = _activeSession!['mitra_display_name'] as String? ?? 'Bestie'; context.push('/chat/session/$sessionId', extra: mitraName); }, ) else ...[ const SizedBox(height: 16), ElevatedButton( style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 16), ), onPressed: () => PricingBottomSheet.show(context), child: const Text('Mulai Curhat', style: TextStyle(fontSize: 18)), ), ], ], ), ), ), ); }, ), ); } } class _ActiveSessionCard extends StatelessWidget { final Map session; final VoidCallback onTap; const _ActiveSessionCard({required this.session, required this.onTap}); @override Widget build(BuildContext context) { final mitraName = session['mitra_display_name'] as String? ?? 'Bestie'; return Card( elevation: 2, child: InkWell( onTap: onTap, borderRadius: BorderRadius.circular(12), child: Padding( padding: const EdgeInsets.all(20), child: Row( children: [ const CircleAvatar( backgroundColor: Colors.green, child: Icon(Icons.chat, color: Colors.white), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Sesi Aktif', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), const SizedBox(height: 4), Text( 'Sedang curhat dengan $mitraName', style: const TextStyle(fontSize: 14, color: Colors.grey), ), ], ), ), const Icon(Icons.chevron_right), ], ), ), ), ); } }