import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:lucide_icons_flutter/lucide_icons.dart'; import '../../../../core/theme/nature_colors.dart'; import '../../../inventory/presentation/product_list_screen.dart'; import '../../../inventory/presentation/daily_rates_screen.dart'; import '../../../inventory/presentation/category_management_screen.dart'; import '../../../sales/presentation/customers_list_screen.dart'; import '../../../sales/presentation/invoices_list_screen.dart'; import '../reports/reports_screen.dart'; import '../../providers/business_provider.dart'; import '../widgets/business_profile_form_sheet.dart'; import '../../../inventory/providers/products_provider.dart'; import '../../../vendor/presentation/vendors_list_screen.dart'; import '../../../vendor/presentation/purchase_orders_list_screen.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; final bool showPurchase = featureState?.purchaseManagement ?? false; return RefreshIndicator( onRefresh: () async { ref.invalidate(businessProfileProvider); ref.invalidate(businessFeatureProvider); }, child: SingleChildScrollView( physics: const AlwaysScrollableScrollPhysics(), 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.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(16), border: Border.all( color: Theme.of( context, ).colorScheme.primary.withValues(alpha: 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), LayoutBuilder( builder: (context, constraints) { final isDesktop = constraints.maxWidth >= 768; final crossAxisCount = isDesktop ? 3 : 2; final childAspectRatio = isDesktop ? 2.2 : 1.25; return GridView.count( crossAxisCount: crossAxisCount, crossAxisSpacing: 16, mainAxisSpacing: 16, shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), childAspectRatio: childAspectRatio, children: [ if (showInventory) ...[ _buildActionCard( context, 'Categories', 'Manage catalog structure', LucideIcons.listTree, Colors.teal, () => Navigator.push( context, MaterialPageRoute( builder: (_) => const CategoryManagementScreen(), ), ), isDesktop: isDesktop, ), _buildActionCard( context, 'Products Catalog', 'View all products', LucideIcons.packageSearch, Colors.blue, () => Navigator.push( context, MaterialPageRoute( builder: (_) => const ProductListScreen(), ), ), isDesktop: isDesktop, ), ], if (showSales) _buildActionCard( context, 'Customers', 'Manage clients', LucideIcons.users, NatureColors.getColor('RECEIVABLES'), () => Navigator.push( context, MaterialPageRoute( builder: (_) => const CustomersListScreen(), ), ), isDesktop: isDesktop, ), if (showPurchase) _buildActionCard( context, 'Vendors', 'Manage suppliers', LucideIcons.truck, Colors.orange, () => Navigator.push( context, MaterialPageRoute( builder: (_) => const VendorsListScreen(), ), ), isDesktop: isDesktop, ), if (showPurchase) _buildActionCard( context, 'Purchases', 'Manage inward stock', LucideIcons.clipboardList, Colors.deepOrange, () => Navigator.push( context, MaterialPageRoute( builder: (_) => const PurchaseOrdersListScreen(), ), ), isDesktop: isDesktop, ), if (showSales) _buildActionCard( context, 'Sales & Invoices', 'Create invoices', LucideIcons.receipt, Colors.green, () => Navigator.push( context, MaterialPageRoute( builder: (_) => const InvoicesListScreen(), ), ), isDesktop: isDesktop, ), if (showInventory) _buildActionCard( context, 'Daily Rates', 'Sync live gold/silver rates', LucideIcons.refreshCw, Colors.amber.shade800, () => Navigator.push( context, MaterialPageRoute( builder: (_) => const DailyRatesScreen(), ), ), isDesktop: isDesktop, ), _buildActionCard( context, 'Reports', 'Analytics & Valuation', LucideIcons.barChart2, Colors.indigo, () => Navigator.push( context, MaterialPageRoute(builder: (_) => const ReportsScreen()), ), isDesktop: isDesktop, ), ], ); }, ), 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.currentStock == null) return false; if (p.currentStock == null) return false; final minStock = 0; return p.currentStock! <= minStock; }).toList(); if (lowStockProducts.isEmpty) { return Container( padding: const EdgeInsets.all(24), decoration: BoxDecoration( color: Colors.grey.withValues(alpha: 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.withValues(alpha: 0.05) : Colors.orange.withValues(alpha: 0.05), borderRadius: BorderRadius.circular(12), border: Border.all( color: isOutOfStock ? Colors.red.withValues(alpha: 0.2) : Colors.orange.withValues(alpha: 0.2), ), ), child: Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: isOutOfStock ? Colors.red.withValues(alpha: 0.1) : Colors.orange.withValues(alpha: 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( 'Out of stock', 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, { bool isDesktop = false, }) { return InkWell( onTap: onTap, borderRadius: BorderRadius.circular(16), child: Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: color.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(16), border: Border.all(color: color.withValues(alpha: 0.2)), ), child: isDesktop ? Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: color.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(10), ), child: Icon(icon, color: color, size: 20), ), const SizedBox(width: 12), Expanded( child: Text( title, style: TextStyle( color: color, fontWeight: FontWeight.bold, fontSize: 16, ), maxLines: 1, overflow: TextOverflow.ellipsis, ), ), Icon( LucideIcons.arrowUpRight, color: color.withValues(alpha: 0.6), size: 16, ), ], ), Text( subtitle, style: TextStyle( color: color.withValues(alpha: 0.8), fontSize: 13, fontWeight: FontWeight.w500, ), maxLines: 1, overflow: TextOverflow.ellipsis, ), ], ) : Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: color.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(10), ), child: Icon(icon, color: color, size: 22), ), const SizedBox(height: 12), Text( title, style: TextStyle( color: color, fontWeight: FontWeight.bold, fontSize: 15, ), maxLines: 1, overflow: TextOverflow.ellipsis, ), const SizedBox(height: 4), Text( subtitle, style: TextStyle( color: color.withValues(alpha: 0.7), fontSize: 12, ), maxLines: 1, overflow: TextOverflow.ellipsis, ), ], ), ), ); } }