import 'package:bookmywages/consts_widgets/app_colors.dart';
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 DrawerMenuVendor extends StatelessWidget {
  final String userName;
  final String userImage;

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

  @override
  Widget build(BuildContext context) {
    // final indexController = InheritedIndex1Controller.of(context);
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.8,
      height: 600,
      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: [
                // 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: 20),

                // Menu Items - Fixed navigation logic
                _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.vendorpackage, arguments: 1);
                  },
                ),

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

                _buildMenuItem(
                  icon: Icons.payment,
                  iconColor: Colors.pink,
                  title: 'Payment',
                  onTap: () {
                    Get.offAllNamed(
                      RouterConts.vendorhistory,
                      arguments: {
                        'historyTab': 2, // Enquiry list tab
                      },
                    );
                  },
                ),

                _buildMenuItem(
                  icon: Icons.person_add_alt_rounded,
                  iconColor: Colors.amber[800]!,
                  title: 'Service Management',
                  onTap: () {
                    Get.offAllNamed(
                      RouterConts.vendorhistory,
                      arguments: {
                        'historyTab': 1, // Enquiry list tab
                      },
                    );
                  },
                ),

                _buildMenuItem(
                  icon: Icons.person,
                  iconColor: AppColors.primary,
                  title: 'Enquiry Management',
                  onTap: () {
                    Get.offAllNamed(
                      RouterConts.vendorhistory,
                      arguments: {
                        'historyTab': 4, // Enquiry list tab
                      },
                    );
                  },
                ),

                _buildMenuItem(
                  icon: Icons.person,
                  iconColor: Colors.green,
                  title: 'My account',
                  onTap: () {
                    Navigator.of(context).pop();
                    Get.toNamed(RouterConts.profilemainvendor);
                  },
                ),

                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,
    );
  }
}
