// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../../app/injection_container.dart';
import '../../core/storage/secure_storage.dart';

// ---------------------------------------------------------------------------
// SplashScreen
// ---------------------------------------------------------------------------
//
// Ditampilkan sesaat setelah user berhasil connect ke server (dari
// ServerConnectScreen) atau saat app dibuka dan serverUrl sudah tersimpan.
//
// Logic:
//   1. Delay singkat 500ms untuk animasi logo.
//   2. Baca accessToken dari SecureStorage.
//   3. Jika tidak ada → redirect /login.
//   4. Jika ada → baca role → redirect /user/walkguide atau /guardian/dashboard.
//
// TTS "Welcome back, {displayName}" jika auto-login (token sudah ada).
// ---------------------------------------------------------------------------

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

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

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  late final AnimationController _animCtrl;
  late final Animation<double> _fadeAnim;

  @override
  void initState() {
    super.initState();
    _animCtrl = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 700));
    _fadeAnim = CurvedAnimation(parent: _animCtrl, curve: Curves.easeIn);
    _animCtrl.forward();
    _route();
  }

  @override
  void dispose() {
    _animCtrl.dispose();
    super.dispose();
  }

  Future<void> _route() async {
    try {
      // Animasi logo selalu tampil minimal 500ms agar tidak langsung flash.
      await Future.delayed(const Duration(milliseconds: 500));

      final storage = sl<SecureStorage>();
      final token =
          await storage.getAccessToken().timeout(const Duration(seconds: 3));
      final role =
          await storage.getUserRole().timeout(const Duration(seconds: 3));

      if (!mounted) return;

      if (token == null || role == null) {
        context.go('/login');
        return;
      }

      // Auto-login: arahkan ke home sesuai role.
      context.go(
          role == 'ROLE_GUARDIAN' ? '/guardian/dashboard' : '/user/walkguide');
    } catch (_) {
      if (mounted) context.go('/login');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF1A56DB),
      body: Center(
        child: FadeTransition(
          opacity: _fadeAnim,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // Logo / icon
              Container(
                width: 100,
                height: 100,
                decoration: BoxDecoration(
                  color: Colors.white.withValues(alpha: 0.12),
                  borderRadius: BorderRadius.circular(28),
                ),
                child: const Icon(
                  Icons.navigation_rounded,
                  color: Colors.white,
                  size: 60,
                ),
              ),
              const SizedBox(height: 24),
              const Text(
                'WalkGuide',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 34,
                  fontWeight: FontWeight.w800,
                  letterSpacing: -0.5,
                ),
              ),
              const SizedBox(height: 6),
              const Text(
                'AI-powered navigation for everyone',
                style: TextStyle(
                  color: Colors.white70,
                  fontSize: 13,
                ),
              ),
              const SizedBox(height: 48),
              const SizedBox(
                width: 28,
                height: 28,
                child: CircularProgressIndicator(
                  color: Colors.white,
                  strokeWidth: 2.5,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
