import 'package:flutter/material.dart';
import 'package:inventory_system/services/session_manager.dart';

enum AppScreen {
  dashboard,
  supplier,
  manufacturer,
  product,
  item,
  station,
  productRequest,
  itemMovement,
  settings,
  productRequestUser,
  itemMovementUser,
  scan,
  profile,
}

class NavBar extends StatelessWidget {
  final bool isAdmin;
  final AppScreen selectedScreen;

  const NavBar({super.key, required this.isAdmin, required this.selectedScreen});

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          DrawerHeader(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.blue.shade700, Colors.blue.shade400],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
            child: Builder(
              builder: (context) {
                final currentUser = SessionManager.instance.currentUser;
                final String displayName = currentUser?['fullName'] ?? 'Not Logged In';

                final bool isSuperAdmin = currentUser?['isSuperAdmin'] ?? false;
                final bool isInventoryMaster = currentUser?['isInventoryMaster'] ?? false;

                String userRole = 'User';
                if (isSuperAdmin) {
                  userRole = 'Administrator';
                } else if (isInventoryMaster) {
                  userRole = 'Inventory Master';
                }

                final String departmentName = currentUser?['department']?['departmentName'] ?? '';

                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    const CircleAvatar(
                      radius: 30,
                      backgroundColor: Colors.white,
                      child: Icon(Icons.person, size: 35, color: Colors.blue),
                    ),
                    const SizedBox(height: 10),
                    Text(
                      displayName, // Use the dynamic display name
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    Text(
                      userRole, // Use the dynamic user role
                      style: const TextStyle(
                        color: Colors.white70,
                        fontSize: 14,
                      ),
                    ),
                    if (departmentName.isNotEmpty)
                      Text(
                        departmentName,
                        style: const TextStyle(
                          color: Colors.white60,
                          fontSize: 12,
                        ),
                      ),
                  ],
                );
              },
            ),
          ),
          if (isAdmin)
            ..._buildAdminListTiles(context)
          else
            ..._buildUserListTiles(context),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.settings),
            title: const Text('Settings'),
            selected: selectedScreen == AppScreen.settings,
            onTap: () {},
          ),
          ListTile(
            leading: const Icon(Icons.logout),
            title: const Text('Logout'),
            onTap: () {
              SessionManager.instance.clearSession();
              Navigator.pushReplacementNamed(context, '/login');
            },
          ),
        ],
      ),
    );
  }

  List<Widget> _buildAdminListTiles(BuildContext context) {
    return [
      ListTile(
        leading: const Icon(Icons.dashboard),
        title: const Text('Dashboard'),
        selected: selectedScreen == AppScreen.dashboard,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.dashboard) {
            Navigator.pushReplacementNamed(context, '/home');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.people),
        title: const Text('Supplier'),
        selected: selectedScreen == AppScreen.supplier,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.supplier) {
            Navigator.pushReplacementNamed(context, '/supplier');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.factory),
        title: const Text('Manufacturer'),
        selected: selectedScreen == AppScreen.manufacturer,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.manufacturer) {
            Navigator.pushReplacementNamed(context, '/manufacturer');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.location_on),
        title: const Text('Station'),
        selected: selectedScreen == AppScreen.station,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.station) {
            Navigator.pushReplacementNamed(context, '/station');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.inventory_2_rounded),
        title: const Text('Product'),
        selected: selectedScreen == AppScreen.product,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.product) {
            Navigator.pushReplacementNamed(context, '/product');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.category_rounded),
        title: const Text('Item'),
        selected: selectedScreen == AppScreen.item,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.item) {
            Navigator.pushReplacementNamed(context, '/item');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.swap_horiz_rounded),
        title: const Text('Item Movement'),
        selected: selectedScreen == AppScreen.itemMovement,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.itemMovement) {
            Navigator.pushReplacementNamed(context, '/item_movement_all');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.assignment_rounded),
        title: const Text('Product Request'),
        selected: selectedScreen == AppScreen.productRequest,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.productRequest) {
            Navigator.pushReplacementNamed(context, '/product_request_t_to_im');
          }
        },
      ),
    ];
  }

  List<Widget> _buildUserListTiles(BuildContext context) {
    return [
      ListTile(
        leading: const Icon(Icons.dashboard),
        title: const Text('Dashboard'),
        selected: selectedScreen == AppScreen.dashboard,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.dashboard) {
            Navigator.pushReplacementNamed(context, '/user-home');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.assignment),
        title: const Text('Product Request'),
        selected: selectedScreen == AppScreen.productRequestUser,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.productRequestUser) {
            Navigator.pushReplacementNamed(context, '/product_request_user');
          }
        },
      ),
      ListTile(
        leading: const Icon(Icons.swap_horiz),
        title: const Text('Item Movement'),
        selected: selectedScreen == AppScreen.itemMovementUser,
        onTap: () {
          Navigator.pop(context);
          if (selectedScreen != AppScreen.itemMovementUser) {
            Navigator.pushReplacementNamed(context, '/item_movement_all_user');
          }
        },
      ),
    ];
  }
}
