import 'package:client_authentication/src/presentation/widgets/common/section_titles.dart';
import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';
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 '../blocs/client_auth_bloc.dart';
import '../blocs/client_auth_event.dart';
import '../blocs/client_auth_state.dart';
import '../widgets/client_sign_in_page/client_sign_in_form.dart';
import '../widgets/common/auth_divider.dart';

/// Page for client users to sign in to their account.
///
/// This page provides email/password authentication as well as social sign-in
/// options via Apple and Google. It matches the design system standards
/// for client-facing authentication flows.
class ClientSignInPage extends StatelessWidget {
  /// Creates a [ClientSignInPage].
  const ClientSignInPage({super.key});

  /// Dispatches the sign in event to the BLoC.
  void _handleSignIn(
    BuildContext context, {
    required String email,
    required String password,
  }) {
    BlocProvider.of<ClientAuthBloc>(
      context,
    ).add(ClientSignInRequested(email: email, password: password));
  }

  @override
  Widget build(BuildContext context) {
    final TranslationsClientAuthenticationSignInPageEn i18n = t.client_authentication.sign_in_page;
    final ClientAuthBloc authBloc = Modular.get<ClientAuthBloc>();

    return BlocProvider<ClientAuthBloc>.value(
      value: authBloc,
      child: BlocConsumer<ClientAuthBloc, ClientAuthState>(
        listener: (BuildContext context, ClientAuthState state) {
          if (state.status == ClientAuthStatus.authenticated) {
            Modular.to.toClientHome();
          } else if (state.status == ClientAuthStatus.error) {
            final String errorMessage = state.errorMessage != null
                ? translateErrorKey(state.errorMessage!)
                : t.errors.generic.unknown;
            UiSnackbar.show(
              context,
              message: errorMessage,
              type: UiSnackbarType.error,
              margin: const EdgeInsets.only(bottom: 120, left: 16, right: 16),
            );
          }
        },
        builder: (BuildContext context, ClientAuthState state) {
          final bool isLoading = state.status == ClientAuthStatus.loading;

          return Scaffold(
            appBar: const UiAppBar(showBackButton: true),
            body: SafeArea(
              bottom: false,
              child: Padding(
                padding: const EdgeInsets.fromLTRB(
                  UiConstants.space6,
                  UiConstants.space8,
                  UiConstants.space6,
                  0,
                ),
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      SectionTitle(title: i18n.title, subtitle: i18n.subtitle),
                      const SizedBox(height: UiConstants.space8),

                      // Sign In Form
                      ClientSignInForm(
                        isLoading: isLoading,
                        onSignIn: ({required String email, required String password}) =>
                            _handleSignIn(
                              context,
                              email: email,
                              password: password,
                            ),
                      ),

                      const SizedBox(height: UiConstants.space6),

                      // Divider
                      AuthDivider(text: i18n.or_divider),

                      const SizedBox(height: UiConstants.space6),
                      
                      /// TODO: FEATURE_NOT_YET_IMPLEMENTED
                      // Social Sign-In Buttons

                      const SizedBox(height: UiConstants.space8),

                      // Sign Up Link
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            i18n.no_account,
                            style: UiTypography.body2r.textSecondary,
                          ),
                          const SizedBox(width: UiConstants.space1),
                          GestureDetector(
                            onTap: () => Modular.to.toClientSignUp(),
                            child: Text(
                              i18n.sign_up_link,
                              style: UiTypography.body2m.textLink,
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: UiConstants.space10),
                    ],
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
