import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/authentication/presentation/login_page.dart';

import '../bloc/auth_bloc.dart';
import '../bloc/auth_state.dart';

/// Routet die App zwischen Bootstrap-Splash, Login-Page und der
/// eigentlichen UI:
/// * `AuthBootstrapping` → Splash (verhindert Login-Page-Flash beim
///   Cold-Start, während der Refresh-Token gegen Keycloak gegengeprüft
///   wird).
/// * `Authenticated` → `child` (= reguläre App).
/// * sonst → LoginPage; `sessionExpired`-Banner wenn ein Refresh
///   serverseitig abgewiesen wurde.
class LoginEnforcer extends StatelessWidget {
  final Widget child;

  const LoginEnforcer({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) {
        if (state is Authenticated) {
          return child;
        }
        if (state is AuthBootstrapping) {
          return const _AuthBootstrapSplash();
        }
        final unauth = state is Unauthenticated ? state : null;
        return LoginPage(
          sessionExpired: unauth?.sessionExpired ?? false,
          loginTimedOut: unauth?.loginTimedOut ?? false,
        );
      },
    );
  }
}

class _AuthBootstrapSplash extends StatelessWidget {
  const _AuthBootstrapSplash();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 60),
              child: Image.asset(
                'assets/holzleitner_Logo_2017_RZ_transparent.png',
              ),
            ),
            const SizedBox(height: 32),
            const CircularProgressIndicator(),
          ],
        ),
      ),
    );
  }
}
