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 '../blocs/client_auth_bloc.dart';
import '../blocs/client_auth_event.dart';
import '../blocs/client_auth_state.dart';
import '../navigation/client_auth_navigator.dart';
import '../widgets/client_sign_up_page/client_sign_up_form.dart';
import '../widgets/common/auth_divider.dart';
import '../widgets/common/auth_social_button.dart';

/// Page for client users to sign up for a new account.
///
/// This page collects company details, email, and password, and offers
/// social sign-up options. It adheres to the design system standards.
class ClientSignUpPage extends StatelessWidget {
  /// Creates a [ClientSignUpPage].
  const ClientSignUpPage({super.key});

  /// Validates inputs and dispatches the sign up event.
  void _handleSignUp(
    BuildContext context, {
    required String companyName,
    required String email,
    required String password,
  }) {
    BlocProvider.of<ClientAuthBloc>(context).add(
      ClientSignUpRequested(
        companyName: companyName,
        email: email,
        password: password,
      ),
    );
  }

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

    return BlocProvider.value(
      value: authBloc,
      child: BlocConsumer<ClientAuthBloc, ClientAuthState>(
        listener: (BuildContext context, ClientAuthState state) {
          if (state.status == ClientAuthStatus.authenticated) {
            Modular.to.navigateClientHome();
          } else if (state.status == ClientAuthStatus.error) {
            final String errorMessage = state.errorMessage != null
                ? translateErrorKey(state.errorMessage!)
                : t.errors.generic.unknown;
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text(errorMessage)),
            );
          }
        },
        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 Up Form
                      ClientSignUpForm(
                        isLoading: isLoading,
                        onSignUp:
                            ({
                              required String companyName,
                              required String email,
                              required String password,
                            }) => _handleSignUp(
                              context,
                              companyName: companyName,
                              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 in register page

                      const SizedBox(height: UiConstants.space8),

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