/// Main Scaffold with Bottom Navigation
///
/// Root navigation wrapper that manages the bottom navigation bar
/// and displays different pages based on the selected tab.
library;

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:worker/core/router/app_router.dart';
import 'package:worker/features/account/presentation/pages/account_page.dart';
import 'package:worker/features/home/presentation/pages/home_page.dart';
import 'package:worker/features/loyalty/presentation/pages/loyalty_page.dart';
import 'package:worker/features/main/presentation/providers/current_page_provider.dart';
import 'package:worker/features/news/presentation/pages/news_list_page.dart';
import 'package:worker/features/notifications/presentation/pages/notifications_page.dart';

/// Main Scaffold Page
///
/// Manages bottom navigation and page switching for:
/// - Home (index 0)
/// - Loyalty (index 1)
/// - News (index 2)
/// - Notifications (index 3)
/// - Account (index 4)
class MainScaffold extends ConsumerWidget {
  const MainScaffold({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final currentIndex = ref.watch(currentPageIndexProvider);
    final colorScheme = Theme.of(context).colorScheme;

    // Define pages
    final pages = [
      const HomePage(),
      const LoyaltyPage(), // Loyalty
      const NewsListPage(),
      const NotificationsPage(), // Notifications
      const AccountPage(), // Account
    ];

    return Scaffold(
      body: IndexedStack(index: currentIndex, children: pages),
      floatingActionButton: currentIndex < 4
          ? Padding(
              padding: const EdgeInsets.only(bottom: 20),
              child: FloatingActionButton(
                onPressed: () => context.push(RouteNames.chat),
                backgroundColor: colorScheme.tertiary,
                elevation: 4,
                child: Icon(
                  Icons.chat_bubble,
                  color: colorScheme.onTertiary,
                  size: 28,
                ),
              ),
            )
          : null,
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: colorScheme.surface,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.05),
              blurRadius: 10,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: SafeArea(
          child: SizedBox(
            height: 70,
            child: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              backgroundColor: colorScheme.surface,
              selectedItemColor: colorScheme.primary,
              unselectedItemColor: colorScheme.onSurfaceVariant,
              selectedFontSize: 11,
              unselectedFontSize: 11,
              iconSize: 24,
              currentIndex: currentIndex,
              elevation: 0,
              items: [
                const BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'Trang chủ',
                ),
                const BottomNavigationBarItem(
                  icon: Icon(Icons.loyalty),
                  label: 'Hội viên',
                ),
                const BottomNavigationBarItem(
                  icon: Icon(Icons.local_offer),
                  label: 'Tin tức',
                ),
                BottomNavigationBarItem(
                  icon: Stack(
                    clipBehavior: Clip.none,
                    children: [
                      const Icon(Icons.notifications),
                      Positioned(
                        top: -4,
                        right: -4,
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 6,
                            vertical: 2,
                          ),
                          decoration: BoxDecoration(
                            color: colorScheme.error,
                            borderRadius: BorderRadius.circular(12),
                          ),
                          constraints: const BoxConstraints(
                            minWidth: 20,
                            minHeight: 20,
                          ),
                          child: Text(
                            '5',
                            style: TextStyle(
                              color: colorScheme.onError,
                              fontSize: 11,
                              fontWeight: FontWeight.w700,
                            ),
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ],
                  ),
                  label: 'Thông báo',
                ),
                const BottomNavigationBarItem(
                  icon: Icon(Icons.account_circle),
                  label: 'Cài đặt',
                ),
              ],
              onTap: (index) {
                ref.read(currentPageIndexProvider.notifier).setIndex(index);
              },
            ),
          ),
        ),
      ),
    );
  }
}
