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-in.
///
/// This widget handles user input for email and password and delegates
/// authentication events to the parent via callbacks.
class ClientSignInForm extends StatefulWidget {

  /// Creates a [ClientSignInForm].
  const ClientSignInForm({
    super.key,
    required this.onSignIn,
    this.isLoading = false,
  });
  /// Callback when the sign-in button is pressed.
  final void Function({required String email, required String password})
  onSignIn;

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

  @override
  State<ClientSignInForm> createState() => _ClientSignInFormState();
}

class _ClientSignInFormState extends State<ClientSignInForm> {
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  bool _obscurePassword = true;

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

  void _handleSubmit() {
    widget.onSignIn(
      email: _emailController.text,
      password: _passwordController.text,
    );
  }

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

    return Column(
      children: <Widget>[
        // Email Field
        UiTextField(
          label: i18n.email_label,
          hintText: i18n.email_hint,
          controller: _emailController,
          keyboardType: TextInputType.emailAddress,
        ),
        const SizedBox(height: UiConstants.space5),

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

        const SizedBox(height: UiConstants.space2),

        // Forgot Password
        Align(
          alignment: Alignment.centerLeft,
          child: GestureDetector(
            onTap: () {},
            child: Text(
              i18n.forgot_password,
              style: UiTypography.body2r.textLink,
            ),
          ),
        ),

        const SizedBox(height: UiConstants.space8),

        // Sign In Button
        UiButton.primary(
          text: widget.isLoading ? null : i18n.sign_in_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,
        ),
      ],
    );
  }
}
