import 'package:flutter/material.dart';

import '../composition/app_config.dart';
import '../composition/app_scope.dart';
import '../routing/app_routes.dart';

class AppShell extends StatelessWidget {
  final String selectedRoute;
  final String title;
  final Widget child;

  const AppShell({
    super.key,
    required this.selectedRoute,
    required this.title,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    final index = _routeToIndex(selectedRoute);
    final config = AppScope.configOf(context);

    return Scaffold(
      appBar: AppBar(
        title: const Text('Kell Creations'),
        actions: [
          _EnvironmentBadge(environment: config.environment),
          const SizedBox(width: 12),
        ],
      ),
      body: Row(
        children: [
          NavigationRail(
            selectedIndex: index,
            onDestinationSelected: (selectedIndex) {
              final route = _indexToRoute(selectedIndex);
              if (route != selectedRoute) {
                Navigator.of(context).pushReplacementNamed(route);
              }
            },
            labelType: NavigationRailLabelType.all,
            minWidth: 88,
            minExtendedWidth: 200,
            leading: const Padding(
              padding: EdgeInsets.symmetric(vertical: 16),
              child: Icon(Icons.storefront_outlined, size: 32),
            ),
            destinations: const [
              NavigationRailDestination(
                icon: Icon(Icons.dashboard_outlined),
                selectedIcon: Icon(Icons.dashboard),
                label: Text('Dashboard'),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.inventory_2_outlined),
                selectedIcon: Icon(Icons.inventory_2),
                label: Text('Inventory'),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.sell_outlined),
                selectedIcon: Icon(Icons.sell),
                label: Text('Products'),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.receipt_long_outlined),
                selectedIcon: Icon(Icons.receipt_long),
                label: Text('Orders'),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.attach_money_outlined),
                selectedIcon: Icon(Icons.attach_money),
                label: Text('Finance'),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.policy_outlined),
                selectedIcon: Icon(Icons.policy),
                label: Text('Policy'),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.hub_outlined),
                selectedIcon: Icon(Icons.hub),
                label: Text('Integrations'),
              ),
            ],
          ),
          const VerticalDivider(width: 1),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(24),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(title, style: Theme.of(context).textTheme.headlineMedium),
                  const SizedBox(height: 8),
                  Expanded(child: child),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  static int _routeToIndex(String route) {
    switch (route) {
      case AppRoutes.dashboard:
        return 0;
      case AppRoutes.inventory:
        return 1;
      case AppRoutes.products:
        return 2;
      case AppRoutes.orders:
        return 3;
      case AppRoutes.finance:
        return 4;
      case AppRoutes.policy:
        return 5;
      case AppRoutes.integrations:
        return 6;
      default:
        return 1;
    }
  }

  static String _indexToRoute(int index) {
    switch (index) {
      case 0:
        return AppRoutes.dashboard;
      case 1:
        return AppRoutes.inventory;
      case 2:
        return AppRoutes.products;
      case 3:
        return AppRoutes.orders;
      case 4:
        return AppRoutes.finance;
      case 5:
        return AppRoutes.policy;
      case 6:
        return AppRoutes.integrations;
      default:
        return AppRoutes.inventory;
    }
  }
}

/// A small coloured chip displayed in the [AppBar] that shows the current
/// runtime environment (e.g. "FAKE" or "WP").
class _EnvironmentBadge extends StatelessWidget {
  final AppEnvironment environment;

  const _EnvironmentBadge({required this.environment});

  @override
  Widget build(BuildContext context) {
    final Color backgroundColor;
    final Color foregroundColor;

    switch (environment) {
      case AppEnvironment.fake:
        backgroundColor = Colors.orange.shade100;
        foregroundColor = Colors.orange.shade900;
      case AppEnvironment.wordpress:
        backgroundColor = Colors.green.shade100;
        foregroundColor = Colors.green.shade900;
    }

    return Center(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
        decoration: BoxDecoration(color: backgroundColor, borderRadius: BorderRadius.circular(4)),
        child: Text(
          environment.label,
          style: Theme.of(context).textTheme.labelSmall?.copyWith(
            color: foregroundColor,
            fontWeight: FontWeight.bold,
            letterSpacing: 1.2,
          ),
        ),
      ),
    );
  }
}
