import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lucide_icons/lucide_icons.dart';
import '../providers/theme_provider.dart'; // Access userProfileProvider
import 'home_screen.dart';
import 'soul_screen.dart';
import 'shop_settings_screen.dart';
import 'placeholders/placeholders.dart';

class MainScreen extends ConsumerStatefulWidget {
  const MainScreen({super.key});

  @override
  ConsumerState<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends ConsumerState<MainScreen> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    // Listen for mode changes to reset navigation to Home
    ref.listen(userProfileProvider.select((value) => value.isBusinessMode), (previous, next) {
      if (previous != next) {
        setState(() {
          _currentIndex = 0;
        });
      }
    });

    final userProfile = ref.watch(userProfileProvider);
    final isBusiness = userProfile.isBusinessMode;

    // Define Screens for each mode
    final List<Widget> screens = isBusiness
        ? [
            const HomeScreen(), // Inventory Management (FAB opens Menu Creation)
            const InstaSupportScreen(), // Social Media Support
            const AnalyticsScreen(), // Analytics
            const ShopSettingsScreen(), // Shop Settings
          ]
        : [
            const HomeScreen(), // My Sake List
            const ScanARScreen(),
            const SommelierScreen(),
            const BreweryMapScreen(),
            const SoulScreen(), // MyPage/Settings
          ];

    // Define Navigation Items
    final List<NavigationDestination> destinations = isBusiness
        ? const [
            NavigationDestination(
               icon: Padding(padding: EdgeInsets.only(bottom: 2), child: Text('🍶', style: TextStyle(fontSize: 22))),
               label: 'ホーム',
            ),
            NavigationDestination(icon: Icon(LucideIcons.instagram), label: '販促'),
            NavigationDestination(icon: Icon(LucideIcons.barChart), label: '分析'),
            NavigationDestination(icon: Icon(LucideIcons.store), label: '店舗'),
          ]
        : const [
            NavigationDestination(
              icon: Padding(padding: EdgeInsets.only(bottom: 2), child: Text('🍶', style: TextStyle(fontSize: 22))),
              label: 'ホーム',
            ),
            NavigationDestination(icon: Icon(LucideIcons.scanLine), label: 'スキャン'),
            NavigationDestination(icon: Icon(LucideIcons.sparkles), label: 'ソムリエ'),
            NavigationDestination(icon: Icon(LucideIcons.map), label: 'マップ'),
            NavigationDestination(icon: Icon(LucideIcons.user), label: 'マイページ'),
          ];

    // Safety: Reset index if out of bounds (shouldn't happen if lengths match)
    if (_currentIndex >= screens.length) _currentIndex = 0;

    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _currentIndex,
          children: screens,
        ),
      ),
      bottomNavigationBar: NavigationBar(
        selectedIndex: _currentIndex,
        onDestinationSelected: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        destinations: destinations,
      ),
    );
  }
}
