import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';

/// A form widget for client sign-up.
///
/// This widget handles user input for company name, email, and password,
/// and delegates registration events to the parent via callbacks.
class ClientSignUpForm extends StatefulWidget {
  /// Callback when the sign-up button is pressed.
  final void Function({
    required String companyName,
    required String email,
    required String password,
  })
  onSignUp;

  /// Whether the authentication is currently loading.
  final bool isLoading;

  /// Creates a [ClientSignUpForm].
  const ClientSignUpForm({
    super.key,
    required this.onSignUp,
    this.isLoading = false,
  });

  @override
  State<ClientSignUpForm> createState() => _ClientSignUpFormState();
}

class _ClientSignUpFormState extends State<ClientSignUpForm> {
  final TextEditingController _companyController = TextEditingController();
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _confirmPasswordController = TextEditingController();
  bool _obscurePassword = true;

  @override
  void dispose() {
    _companyController.dispose();
    _emailController.dispose();
    _passwordController.dispose();
    _confirmPasswordController.dispose();
    super.dispose();
  }

  void _handleSubmit() {
    if (_passwordController.text != _confirmPasswordController.text) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('Passwords do not match')));
      return;
    }

    widget.onSignUp(
      companyName: _companyController.text,
      email: _emailController.text,
      password: _passwordController.text,
    );
  }

  @override
  Widget build(BuildContext context) {
    final TranslationsClientAuthenticationSignUpPageEn i18n = t.client_authentication.sign_up_page;

    return Column(
      children: <Widget>[
        // Company Name Field
        UiTextField(
          label: i18n.company_label,
          hintText: i18n.company_hint,
          controller: _companyController,
          textInputAction: TextInputAction.next,
        ),
        const SizedBox(height: UiConstants.space4),

        // Email Field
        UiTextField(
          label: i18n.email_label,
          hintText: i18n.email_hint,
          controller: _emailController,
          keyboardType: TextInputType.emailAddress,
          textInputAction: TextInputAction.next,
        ),
        const SizedBox(height: UiConstants.space4),

        // Password Field
        UiTextField(
          label: i18n.password_label,
          hintText: i18n.password_hint,
          controller: _passwordController,
          obscureText: _obscurePassword,
          textInputAction: TextInputAction.next,
          suffix: IconButton(
            icon: Icon(
              _obscurePassword ? UiIcons.eyeOff : UiIcons.eye,
              color: UiColors.iconSecondary,
              size: 20,
            ),
            onPressed: () =>
                setState(() => _obscurePassword = !_obscurePassword),
          ),
        ),
        const SizedBox(height: UiConstants.space4),

        // Confirm Password Field
        UiTextField(
          label: i18n.confirm_password_label,
          hintText: i18n.confirm_password_hint,
          controller: _confirmPasswordController,
          obscureText: _obscurePassword,
          textInputAction: TextInputAction.done,
          onSubmitted: (_) => _handleSubmit(),
        ),

        const SizedBox(height: UiConstants.space8),

        // Create Account Button
        UiButton.primary(
          text: widget.isLoading ? null : i18n.create_account_button,
          onPressed: widget.isLoading ? null : _handleSubmit,
          fullWidth: true,
          child: widget.isLoading
              ? const SizedBox(
                  height: 24,
                  width: 24,
                  child: CircularProgressIndicator(
                    color: UiColors.white,
                    strokeWidth: 2,
                  ),
                )
              : null,
        ),
      ],
    );
  }
}
