import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';

import '../../core/routing/routes.dart';
import '../providers/userRoleProvider.dart';

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

  @override
  ConsumerState<MobileBottomMenu> createState() => _MobileBottomMenuState();
}

class _MobileBottomMenuState extends ConsumerState<MobileBottomMenu> {
  int _currentIndex = 0;

  void _showOptionsSheet(List<Map<String, dynamic>> options) {
    showModalBottomSheet(
      context: context,
      backgroundColor: const Color(0xFFDDF5F1),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      builder: (_) => ListView.separated(
        padding: const EdgeInsets.all(16),
        shrinkWrap: true,
        itemCount: options.length,
        separatorBuilder: (_, __) => const SizedBox(height: 8),
        itemBuilder: (_, index) {
          final item = options[index];
          return ListTile(
            title: Text(
              item['label'],
              style: GoogleFonts.inter(
                fontSize: 16,
                fontWeight: FontWeight.w500,
              ),
            ),
            trailing: const Icon(Icons.arrow_forward_ios_rounded, size: 16),
            onTap: () {
              Navigator.pop(context);
              context.go(item['route']);
            },
          );
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final roleId = ref.watch(userRoleProvider);

    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      backgroundColor: const Color(0xFF425B5B),
      selectedItemColor: Colors.white,
      unselectedItemColor: Colors.white70,
      currentIndex: _currentIndex,
      onTap: (index) {
        setState(() => _currentIndex = index);

        switch (index) {
          case 0:
            context.go(AppRoutes.dashboard);
            break;

          case 1:
            // Reports tapped → show bottom sheet
            _showOptionsSheet([
              {'label': 'Claims', 'route': AppRoutes.claimlist},
              {'label': 'Endorsement', 'route': AppRoutes.Endorsement},
              {'label': 'Policy', 'route': AppRoutes.policylist},
            ]);
            break;

          case 2:
            // Enquiry
            if (roleId == 'agent') {
              context.go(AppRoutes.enquiryLst);
            } else {
              context.go(AppRoutes.enquiryForStaff);
            }
            break;

          // case 3:
          //   // User options only for manager
          //   if (roleId == 'manager') {
          //     _showOptionsSheet([
          //       {'label': 'Agent', 'route': AppRoutes.agentLst},
          //       {'label': 'Staff', 'route': AppRoutes.staffLst},
          //     ]);
          //   }
          //   break;

          case 3:
            context.go(AppRoutes.profile);
            break;
        }
      },
      items: [
        BottomNavigationBarItem(
          icon: _currentIndex == 0
              ? Image.asset(
                  "assets/drawer/drawerImg1_light.png",
                  height: 20,
                  width: 20,
                )
              : Image.asset(
                  "assets/drawer/drawerImg1.png",
                  height: 20,
                  width: 20,
                ),
          label: "Dashboard",
        ),
        BottomNavigationBarItem(
          icon: _currentIndex == 1
              ? Image.asset(
                  "assets/drawer/drawerImg2_light.png",
                  height: 20,
                  width: 20,
                )
              : Image.asset(
                  "assets/drawer/drawerImg2.png",
                  height: 20,
                  width: 20,
                ),
          label: "Reports",
        ),
        BottomNavigationBarItem(
          icon: Icon(
            Icons.list_alt_rounded,
            size: 24,
            color: _currentIndex == 2 ? Colors.white : Colors.black,
          ),
          label: "Enquiry",
        ),
        // if (roleId == 'manager') ...[
        //   BottomNavigationBarItem(
        //     icon: Icon(
        //       Icons.person_add_alt,
        //       size: 24,
        //       color: _currentIndex == 3 ? Colors.white : Colors.black,
        //     ),
        //     label: "User",
        //   ),
        // ],
        BottomNavigationBarItem(
          icon: Icon(
            Icons.person_outline,
            size: 24,
            color: _currentIndex == 3 ? Colors.white : Colors.black,
          ),
          label: "Profile",
        ),
      ],
    );
  }
}
