import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'auth_storage.dart';
import 'screens/login_screen.dart';
import 'screens/dashboard_screen.dart';

// Driver app brand (dark theme)
const _driverBg = Color(0xFF0F1114);
const _driverPrimary = Color(0xFFFF9500);

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(const JamboDriverApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Jambo Driver',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.dark(
          primary: _driverPrimary,
          surface: _driverBg,
          error: const Color(0xFFFF4545),
        ),
        scaffoldBackgroundColor: _driverBg,
        useMaterial3: true,
      ),
      initialRoute: '/splash',
      routes: {
        '/splash': (context) => const _SplashScreen(),
        '/login': (context) => const LoginScreen(),
        '/dashboard': (context) => const DashboardScreen(),
      },
    );
  }
}

class _SplashScreen extends StatefulWidget {
  const _SplashScreen();

  @override
  State<_SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<_SplashScreen> {
  @override
  void initState() {
    super.initState();
    _checkAuth();
  }

  Future<void> _checkAuth() async {
    await Future.delayed(const Duration(milliseconds: 300));
    if (!mounted) return;
    final token = await AuthStorage.getToken();
    if (token != null && token.isNotEmpty) {
      Navigator.of(context).pushReplacementNamed('/dashboard');
    } else {
      Navigator.of(context).pushReplacementNamed('/login');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: _driverBg,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.delivery_dining_rounded, size: 64, color: _driverPrimary),
            const SizedBox(height: 16),
            Text(
              'Jambo Driver',
              style: TextStyle(
                color: const Color(0xFFF2F2F2),
                fontSize: 24,
                fontWeight: FontWeight.w700,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
