import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/providers/auth_provider.dart';
import '../providers/dashboard_providers.dart';

class DashboardShell extends ConsumerWidget {
  final PreferredSizeWidget? appBar;
  final Widget body;
  final Widget? floatingActionButton;
  final Color? backgroundColor;

  const DashboardShell({
    super.key,
    this.appBar,
    required this.body,
    this.floatingActionButton,
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final location = GoRouterState.of(context).matchedLocation;
    final employee = ref.watch(authProvider).employee;

    int currentIndex = 0;
    if (location.startsWith('/dashboard/vehicles')) currentIndex = 1;
    if (location.startsWith('/dashboard/reservations')) currentIndex = 2;
    if (location.startsWith('/dashboard/customers')) currentIndex = 3;

    return Scaffold(
      appBar: appBar,
      backgroundColor: backgroundColor,
      drawer: _Drawer(employee: employee),
      body: body,
      floatingActionButton: floatingActionButton,
      bottomNavigationBar: NavigationBar(
        selectedIndex: currentIndex,
        onDestinationSelected: (i) {
          switch (i) {
            case 0:
              context.go('/dashboard');
            case 1:
              context.go('/dashboard/vehicles');
            case 2:
              context.go('/dashboard/reservations');
            case 3:
              context.go('/dashboard/customers');
          }
        },
        destinations: const [
          NavigationDestination(
            icon: Icon(Icons.dashboard_outlined),
            selectedIcon: Icon(Icons.dashboard),
            label: 'Dashboard',
          ),
          NavigationDestination(
            icon: Icon(Icons.directions_car_outlined),
            selectedIcon: Icon(Icons.directions_car),
            label: 'Vehicles',
          ),
          NavigationDestination(
            icon: Icon(Icons.event_note_outlined),
            selectedIcon: Icon(Icons.event_note),
            label: 'Reservations',
          ),
          NavigationDestination(
            icon: Icon(Icons.people_outlined),
            selectedIcon: Icon(Icons.people),
            label: 'Customers',
          ),
        ],
      ),
    );
  }
}

class _Drawer extends ConsumerWidget {
  final dynamic employee;

  const _Drawer({required this.employee});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final unreadAsync = ref.watch(unreadCountProvider);
    final unread = unreadAsync.valueOrNull ?? 0;

    return Drawer(
      child: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.all(20),
              child: Row(
                children: [
                  CircleAvatar(
                    backgroundColor: const Color(0xFF1A56DB),
                    radius: 24,
                    child: Text(
                      employee?.firstName.substring(0, 1).toUpperCase() ?? 'U',
                      style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          employee?.fullName ?? 'Employee',
                          style: const TextStyle(fontWeight: FontWeight.w600),
                        ),
                        Text(
                          employee?.role ?? '',
                          style: const TextStyle(
                            fontSize: 12,
                            color: Color(0xFF6B7280),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            const Divider(),
            Expanded(
              child: ListView(
                padding: EdgeInsets.zero,
                children: [
                  _NavItem(
                    icon: Icons.dashboard_outlined,
                    label: 'Dashboard',
                    onTap: () {
                      Navigator.pop(context);
                      context.go('/dashboard');
                    },
                  ),
                  _NavItem(
                    icon: Icons.directions_car_outlined,
                    label: 'Vehicles',
                    onTap: () {
                      Navigator.pop(context);
                      context.go('/dashboard/vehicles');
                    },
                  ),
                  _NavItem(
                    icon: Icons.event_note_outlined,
                    label: 'Reservations',
                    onTap: () {
                      Navigator.pop(context);
                      context.go('/dashboard/reservations');
                    },
                  ),
                  _NavItem(
                    icon: Icons.people_outlined,
                    label: 'Customers',
                    onTap: () {
                      Navigator.pop(context);
                      context.go('/dashboard/customers');
                    },
                  ),
                  const Divider(),
                  _NavItem(
                    icon: Icons.public_outlined,
                    label: 'Online Requests',
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/online-reservations');
                    },
                  ),
                  _NavItem(
                    icon: Icons.description_outlined,
                    label: 'Contracts',
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/contracts');
                    },
                  ),
                  _NavItem(
                    icon: Icons.notifications_outlined,
                    label: 'Notifications',
                    badge: unread > 0 ? '$unread' : null,
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/notifications');
                    },
                  ),
                  _NavItem(
                    icon: Icons.local_offer_outlined,
                    label: 'Offers',
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/offers');
                    },
                  ),
                  _NavItem(
                    icon: Icons.bar_chart_outlined,
                    label: 'Reports',
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/reports');
                    },
                  ),
                  _NavItem(
                    icon: Icons.people_outline,
                    label: 'Team',
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/team');
                    },
                  ),
                  _NavItem(
                    icon: Icons.settings_outlined,
                    label: 'Settings',
                    onTap: () {
                      Navigator.pop(context);
                      context.push('/dashboard/settings');
                    },
                  ),
                ],
              ),
            ),
            const Divider(),
            ListTile(
              leading: const Icon(Icons.logout, color: Color(0xFFE02424)),
              title: const Text(
                'Sign Out',
                style: TextStyle(color: Color(0xFFE02424)),
              ),
              onTap: () async {
                final router = GoRouter.of(context);
                Navigator.of(context).pop();
                await ref.read(authProvider.notifier).logout();
                router.go('/landing');
              },
            ),
            const SizedBox(height: 8),
          ],
        ),
      ),
    );
  }
}

class _NavItem extends StatelessWidget {
  final IconData icon;
  final String label;
  final String? badge;
  final VoidCallback onTap;

  const _NavItem({
    required this.icon,
    required this.label,
    this.badge,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: badge != null
          ? Badge(label: Text(badge!), child: Icon(icon))
          : Icon(icon),
      title: Text(label),
      onTap: onTap,
    );
  }
}
