import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:lucide_icons/lucide_icons.dart'; import '../../../../core/theme/nature_colors.dart'; import '../../../inventory/presentation/product_list_screen.dart'; import '../../../inventory/presentation/quick_adjust_stock_screen.dart'; import '../../../inventory/presentation/uoms_list_screen.dart'; import '../../../sales/presentation/customers_list_screen.dart'; import '../../../sales/presentation/invoices_list_screen.dart'; import '../../providers/business_provider.dart'; import '../widgets/business_profile_form_sheet.dart'; import '../../../inventory/providers/products_provider.dart'; class BusinessHubScreen extends ConsumerWidget { const BusinessHubScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final businessState = ref.watch(businessProfileProvider); final profile = businessState.value; final title = (profile?.businessName != null && profile!.businessName.isNotEmpty) ? profile.businessName : 'Business Overview'; final subtitle = (profile?.address != null && profile!.address!.isNotEmpty) ? profile.address! : 'Manage your inventory and stock'; final featureState = ref.watch(businessFeatureProvider).value; final bool showInventory = featureState?.inventoryManagement ?? false; final bool showSales = featureState?.salesManagement ?? false; return SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, backgroundColor: Colors.transparent, builder: (context) => const BusinessProfileFormSheet(), ); }, borderRadius: BorderRadius.circular(16), child: Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Theme.of(context).colorScheme.primary.withOpacity(0.1), borderRadius: BorderRadius.circular(16), border: Border.all(color: Theme.of(context).colorScheme.primary.withOpacity(0.2)), ), child: Row( children: [ Icon(LucideIcons.briefcase, color: Theme.of(context).colorScheme.primary, size: 32), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title, style: Theme.of(context).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold)), Text(subtitle, style: const TextStyle(color: Colors.grey)), ], ), ), ], ), ), ), const SizedBox(height: 24), GridView.count( crossAxisCount: 2, crossAxisSpacing: 16, mainAxisSpacing: 16, shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), childAspectRatio: 1.2, children: [ if (showInventory) ...[ _buildActionCard( context, 'Products Catalog', 'View all products', LucideIcons.packageSearch, Colors.blue, () => Navigator.push(context, MaterialPageRoute(builder: (_) => const ProductListScreen())), ), _buildActionCard( context, 'Adjust Stock', 'Quick Stock Edit', LucideIcons.arrowRightLeft, Colors.orange, () => Navigator.push(context, MaterialPageRoute(builder: (_) => const QuickAdjustStockScreen())), ), _buildActionCard( context, 'Units of Measure', 'Manage measurements', LucideIcons.ruler, Colors.purple, () => Navigator.push(context, MaterialPageRoute(builder: (_) => const UomsListScreen())), ), ], if (showSales) ...[ _buildActionCard( context, 'Sales & Invoices', 'Create invoices', LucideIcons.receipt, Colors.green, () => Navigator.push(context, MaterialPageRoute(builder: (_) => const InvoicesListScreen())), ), _buildActionCard( context, 'Customers', 'Manage clients', LucideIcons.users, NatureColors.getColor('RECEIVABLES'), () => Navigator.push(context, MaterialPageRoute(builder: (_) => const CustomersListScreen())), ), ], ], ), if (showInventory) ...[ const SizedBox(height: 32), Text('Low Stock Alerts', style: Theme.of(context).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold)), Consumer( builder: (context, ref, child) { final productsState = ref.watch(productsProvider); return productsState.when( data: (products) { final lowStockProducts = products.where((p) { if (!(p.trackInventory ?? false)) return false; if (p.currentStock == null) return false; final minStock = p.minStock ?? 0; return p.currentStock! <= minStock; }).toList(); if (lowStockProducts.isEmpty) { return Container( padding: const EdgeInsets.all(24), decoration: BoxDecoration( color: Colors.grey.withOpacity(0.05), borderRadius: BorderRadius.circular(16), ), child: const Center( child: Text('All products are adequately stocked.', style: TextStyle(color: Colors.grey)), ), ); } return ListView.separated( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: lowStockProducts.length > 5 ? 5 : lowStockProducts.length, separatorBuilder: (context, index) => const SizedBox(height: 12), itemBuilder: (context, index) { final product = lowStockProducts[index]; final isOutOfStock = product.currentStock! <= 0; return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: isOutOfStock ? Colors.red.withOpacity(0.05) : Colors.orange.withOpacity(0.05), borderRadius: BorderRadius.circular(12), border: Border.all(color: isOutOfStock ? Colors.red.withOpacity(0.2) : Colors.orange.withOpacity(0.2)), ), child: Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: isOutOfStock ? Colors.red.withOpacity(0.1) : Colors.orange.withOpacity(0.1), shape: BoxShape.circle, ), child: Icon( LucideIcons.alertTriangle, color: isOutOfStock ? Colors.red : Colors.orange, size: 20, ), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(product.name, style: const TextStyle(fontWeight: FontWeight.bold)), if (product.sku != null && product.sku!.isNotEmpty) Text('SKU: ${product.sku}', style: TextStyle(fontSize: 12, color: Colors.grey.shade600)), ], ), ), Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( '${product.currentStock} left', style: TextStyle( fontWeight: FontWeight.bold, color: isOutOfStock ? Colors.red : Colors.orange.shade800, ), ), Text( 'Min: ${product.minStock ?? 0}', style: TextStyle(fontSize: 12, color: Colors.grey.shade600), ), ], ), ], ), ); }, ); }, loading: () => const Center(child: CircularProgressIndicator()), error: (error, stack) => Center(child: Text('Error: $error')), ); }, ), ], ], ), ); } Widget _buildActionCard(BuildContext context, String title, String subtitle, IconData icon, Color color, VoidCallback onTap) { return GestureDetector( onTap: onTap, child: Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: color.withOpacity(0.1), borderRadius: BorderRadius.circular(16), border: Border.all(color: color.withOpacity(0.2)), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(icon, color: color, size: 28), const SizedBox(height: 12), Text(title, style: TextStyle(color: color, fontWeight: FontWeight.bold, fontSize: 16)), const SizedBox(height: 4), Text(subtitle, style: TextStyle(color: color.withOpacity(0.7), fontSize: 12)), ], ), ), ); } }