import 'package:bookmywages/routers/consts_router.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:shared_preferences/shared_preferences.dart';

class DrawerMenu extends StatelessWidget {
  final String userName;
  final String userImage;

  const DrawerMenu({
    super.key,
    required this.userName,
    required this.userImage,
  });

  @override
  Widget build(BuildContext context) {
    // final indexController = InheritedIndexController.of(context);
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.8,
      height: 580,
      child: GestureDetector(
        // This captures horizontal drag gestures for the entire drawer
        onHorizontalDragEnd: (details) {
          if (details.primaryVelocity! < 0) {
            // Close drawer only when swiped left (negative velocity)
            Navigator.of(context).pop();
          }
        },
        child: Drawer(
          backgroundColor: Colors.white,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(50),
              bottomRight: Radius.circular(50),
            ),
          ),
          child: SafeArea(
            child: Column(
              children: [
                const SizedBox(height: 16),
                // User Profile Section
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    children: [
                      CircleAvatar(
                        radius: 24,
                        backgroundImage: NetworkImage(userImage),
                      ),
                      const SizedBox(width: 16),
                      Text(
                        userName,
                        style: const TextStyle(
                          fontFamily: 'Gilroy-Bold',
                          fontSize: 18,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 30),

                // Menu Items - Completely removed Navigator.pop
                _buildMenuItem(
                  icon: Icons.home,
                  iconColor: Colors.orange,
                  title: 'Dashboard',
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                ),

                _buildMenuItem(
                  icon: Icons.card_membership,
                  iconColor: Colors.blue,
                  title: 'Package/Subscription',
                  onTap: () {
                    Get.offAllNamed(RouterConts.packageList, arguments: 1);
                  },
                ),

                _buildMenuItem(
                  icon: Icons.book,
                  iconColor: Colors.deepPurple,
                  title: 'Book Services',
                  onTap: () {
                    Get.offAllNamed(
                      RouterConts.history,
                      arguments: {
                        'historyTab': 0, // Enquiry list tab
                      },
                    );
                  },
                ),

                _buildMenuItem(
                  icon: Icons.history,
                  iconColor: Colors.pink,
                  title: 'History',
                  onTap: () {
                    Get.offAllNamed(
                      RouterConts.history,
                      arguments: {
                        'historyTab': 0, // Enquiry list tab
                      },
                    );
                  },
                ),

                _buildMenuItem(
                  icon: Icons.notifications,
                  iconColor: Colors.amber[800]!,
                  title: 'Notification',
                  onTap: () {
                    // Only handle navigation logic here
                    // DO NOT close drawer
                  },
                ),

                _buildMenuItem(
                  icon: Icons.person,
                  iconColor: Colors.green,
                  title: 'My account',
                  onTap: () {
                    Get.offAllNamed(RouterConts.profilemainscreen);
                  },
                ),

                const Spacer(),

                // Sign Out
                _buildMenuItem(
                  icon: Icons.exit_to_app,
                  iconColor: Colors.indigo,
                  title: 'Sign out',
                  onTap: () async {
                    final prefs = await SharedPreferences.getInstance();
                    await prefs.remove('userId');
                    await prefs.remove('vendor_id');
                    await prefs.remove('data');

                    // Using GetX navigation
                    Get.offAllNamed(RouterConts.loginpage);
                  },
                ),

                const SizedBox(height: 24),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildMenuItem({
    required IconData icon,
    required Color iconColor,
    required String title,
    required VoidCallback onTap,
  }) {
    return ListTile(
      leading: Container(
        width: 36,
        height: 36,
        decoration: BoxDecoration(
          color: iconColor.withOpacity(0.1),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Icon(icon, color: iconColor, size: 20),
      ),
      title: Text(
        title,
        style: const TextStyle(
          fontFamily: 'Gilroy-Medium',
          fontSize: 16,
          fontWeight: FontWeight.w500,
        ),
      ),
      onTap: onTap,
    );
  }
}
