import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:krow_core/core.dart';
import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';

import '../blocs/client_settings_bloc.dart';
import '../widgets/client_settings_page/settings_actions.dart';
import '../widgets/client_settings_page/settings_profile_header.dart';

/// Page for client settings and profile management.
///
/// This page follows the KROW architecture by being a [StatelessWidget]
/// and delegating its state management to [ClientSettingsBloc] and its
/// UI sections to specialized sub-widgets.
class ClientSettingsPage extends StatelessWidget {
  /// Creates a [ClientSettingsPage].
  const ClientSettingsPage({super.key});

  @override
  /// Builds the client settings page UI.
  Widget build(BuildContext context) {
    return BlocProvider<ClientSettingsBloc>(
      create: (BuildContext context) => Modular.get<ClientSettingsBloc>(),
      child: BlocListener<ClientSettingsBloc, ClientSettingsState>(
        listener: (BuildContext context, ClientSettingsState state) {
          if (state is ClientSettingsSignOutSuccess) {
            UiSnackbar.show(
              context,
              message: 'Signed out successfully',
              type: UiSnackbarType.success,
            );
            Modular.to.toClientRoot();
          }
          if (state is ClientSettingsError) {
            UiSnackbar.show(
              context,
              message: translateErrorKey(state.message),
              type: UiSnackbarType.error,
            );
          }
        },
        child: const Scaffold(
          body: CustomScrollView(
            slivers: <Widget>[
              SettingsProfileHeader(),
              SettingsActions(),
            ],
          ),
        ),
      ),
    );
  }
}
