import 'package:flutter/material.dart';

import 'config/theme.dart';
import 'core/services/auth_service.dart';
import 'core/services/connectivity_service.dart';
import 'core/services/deep_link_service.dart';
import 'core/services/push_service.dart';
import 'features/shell/app_shell.dart';
import 'features/splash/splash_screen.dart';

class El3abApp extends StatefulWidget {
  final AuthService authService;
  final PushService pushService;
  final DeepLinkService deepLinkService;
  final ConnectivityService connectivityService;

  const El3abApp({
    super.key,
    required this.authService,
    required this.pushService,
    required this.deepLinkService,
    required this.connectivityService,
  });

  @override
  State<El3abApp> createState() => _El3abAppState();
}

class _El3abAppState extends State<El3abApp> {
  bool _showSplash = true;
  String? _biometricToken;

  void _onSplashComplete() {
    setState(() => _showSplash = false);
  }

  void _onBiometricSuccess() async {
    final token = await widget.authService.getToken();
    setState(() {
      _biometricToken = token;
      _showSplash = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'EL3AB',
      debugShowCheckedModeBanner: false,
      theme: El3abTheme.dark,
      locale: const Locale('ar'),
      builder: (context, child) {
        return Directionality(
          textDirection: TextDirection.rtl,
          child: child!,
        );
      },
      home: _showSplash
          ? SplashScreen(
              authService: widget.authService,
              onComplete: _onSplashComplete,
              onBiometricSuccess: _onBiometricSuccess,
            )
          : AppShell(
              authService: widget.authService,
              pushService: widget.pushService,
              deepLinkService: widget.deepLinkService,
              connectivityService: widget.connectivityService,
              initialToken: _biometricToken,
            ),
    );
  }
}
