import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../core/app_colors.dart';
import '../services/auth_service.dart';
import '../services/route_simulator_service.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});
  @override State<SplashScreen> createState() => _SplashScreenState();
}

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

  Future<void> _go() async {
    await Future.delayed(const Duration(seconds: 2));
    if (!mounted) return;
    final auth = context.read<AuthService>();
    context.read<RouteSimulatorService>().startAllRoutes();
    if (auth.isLoggedIn) {
      _navigate(auth.rol);
    } else {
      Navigator.pushReplacementNamed(context, '/login');
    }
  }

  void _navigate(String rol) {
    switch (rol) {
      case 'ADMINISTRADOR': Navigator.pushReplacementNamed(context, '/admin'); break;
      case 'CONDUCTOR': Navigator.pushReplacementNamed(context, '/driver'); break;
      default: Navigator.pushReplacementNamed(context, '/home'); break;
    }
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    backgroundColor: AppColors.guindaPrimary,
    body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
      Container(width:100,height:100,
        decoration:BoxDecoration(color:Colors.white12,shape:BoxShape.circle,
          border:Border.all(color:AppColors.dorado,width:3)),
        child:const Icon(Icons.delete_sweep_rounded,size:52,color:AppColors.dorado)),
      const SizedBox(height:20),
      const Text('CELAYA LIMPIA',style:TextStyle(color:Colors.white,fontSize:26,
          fontWeight:FontWeight.bold,letterSpacing:2)),
      const SizedBox(height:4),
      const Text('H. Ayuntamiento de Celaya',style:TextStyle(color:Colors.white60,fontSize:13)),
      const SizedBox(height:40),
      const CircularProgressIndicator(valueColor:AlwaysStoppedAnimation<Color>(AppColors.dorado)),
    ])),
  );
}
