import 'package:flutter/material.dart';
import 'screens/splash_screen.dart';
import 'screens/login_screen.dart';
import 'screens/user/home_screen/home_screen_user.dart';
import 'screens/user/product_request/product_request_user.dart';
import 'screens/user/item_movement/item_movement_all_user.dart';
import 'screens/user/item_movement/item_movement_item_user.dart';
import 'screens/user/item_movement/item_movement_station_user.dart';
import 'screens/user/scan/scan_user.dart';
import 'screens/admin/home_screen/home_screen.dart';
import 'screens/admin/supplier/supplier.dart';
import 'screens/admin/manufacturer/manufacturer.dart';
import 'screens/admin/station/station.dart';
import 'screens/admin/product/product.dart';
import 'screens/admin/item/item.dart';
import 'screens/admin/scan/scan.dart';
import 'screens/admin/item_movement/item_movement_all.dart';
import 'screens/admin/item_movement/item_movement_item.dart';
import 'screens/admin/item_movement/item_movement_station.dart';
import 'screens/admin/product_request/technician_to_invMaster.dart';
import 'screens/admin/product_request/invMaster_to_invMaster.dart';
import 'screens/admin/product_request/product_request_form.dart';
import 'screens/profile/profile_screen.dart'; // Add this import
import 'routes/slide_route.dart';
import 'package:inventory_system/services/api_service.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ApiService.init();
  runApp(const InventorySystemApp());
}

class InventorySystemApp extends StatelessWidget {
  const InventorySystemApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Inventory System',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.red,
        primaryColor: const Color(0xFF8B0000),
        scaffoldBackgroundColor: Colors.white,
        fontFamily: 'Roboto',
        inputDecorationTheme: InputDecorationTheme(
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: BorderSide(color: Colors.grey.shade300),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: BorderSide(color: Colors.grey.shade300),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Color(0xFF8B0000), width: 2),
          ),
          filled: true,
          fillColor: Colors.white,
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: const Color(0xFF8B0000),
            foregroundColor: Colors.white,
            padding: const EdgeInsets.symmetric(vertical: 16),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8),
            ),
            elevation: 0,
          ),
        ),
      ),
      initialRoute: '/',
      // Use a single onGenerateRoute so we can control transition per route
      onGenerateRoute: (RouteSettings settings) {
        final name = settings.name;
        Widget? page;
        // Map route names to pages
        switch (name) {
          case '/':
            page = const SplashScreen();
            break;
          case '/login':
            page = const LoginScreen();
            break;
          case '/home':
            page = const HomeScreen();
            break;
          case '/user-home':
            page = const UserHomeScreen();
            break;
          case '/supplier':
            page = const SupplierScreen();
            break;
          case '/manufacturer':
            page = const ManufacturerScreen();
            break;
          case '/station':
            page = const StationScreen();
            break;
          case '/product':
            page = const ProductScreen();
            break;
          case '/item':
            page = const ItemScreen();
            break;
          case '/scan':
            page = const ScanScreen();
            break;
          case '/scan-user':
            page = const ScanUserScreen();
            break;
          case '/profile':
            page = const ProfileScreen();
            break;
          case '/item_movement_all':
            page = const ItemMovementAllScreen();
            break;
          case '/item_movement_item':
            page = ItemMovementItemScreen();
            break;
          case '/item_movement_station':
            page = ItemMovementStationScreen();
            break;
          case '/product_request_t_to_im':
            page = const TechnicianToInvMasterScreen();
            break;
          case '/product_request_im_to_im':
            page = const InvMasterToInvMasterScreen();
            break;
          case '/product_request_form':
            page = const ProductRequestFormScreen();
            break;
          case '/product_request_user':
            page = const ProductRequestUserScreen();
            break;
          case '/item_movement_all_user':
            page = const ItemMovementAllUserScreen();
            break;
          case '/item_movement_item_user':
            page = const ItemMovementItemUserScreen();
            break;
          case '/item_movement_station_user':
            page = const ItemMovementStationUserScreen();
            break;
        }

        if (page == null) {
          // Unknown route: fallback to default behavior
          return MaterialPageRoute(builder: (_) => const SplashScreen());
        }

        // Exclusion list: use default MaterialPageRoute (no slide)
        final excluded = <String>{
          '/login',
          '/scan',
          '/product_request_form',
          // Any other add/edit forms can be added here if you wire them as named routes later
        };

        if (excluded.contains(name)) {
          return MaterialPageRoute(builder: (_) => page!, settings: settings);
        }

        // Default: slide transition
        return createSlideRoute(page);
      },
    );
  }
}