import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../ui_typography.dart';
import '../ui_constants.dart';
import '../ui_colors.dart';

/// A custom TextField for the Krow UI design system.
///
/// This widget combines a label and a [TextField] with consistent styling.
class UiTextField extends StatelessWidget {
  /// The label text to display above the text field.
  final String? label;

  /// The hint text to display inside the text field when empty.
  final String? hintText;

  /// Signature for the callback that is called when the text in the field changes.
  final ValueChanged<String>? onChanged;

  /// The controller for the text field.
  final TextEditingController? controller;

  /// The type of keyboard to use for editing the text.
  final TextInputType? keyboardType;

  /// The maximum number of lines for the text field. Defaults to 1.
  final int? maxLines;

  /// Whether to hide the text being edited (e.g., for passwords). Defaults to false.
  final bool obscureText;

  /// The type of action button to use for the keyboard.
  final TextInputAction? textInputAction;

  /// Signature for the callback that is called when the user submits the text field.
  final ValueChanged<String>? onSubmitted;

  /// Whether the text field should be focused automatically. Defaults to false.
  final bool autofocus;

  /// Optional input formatters to validate or format the text as it is typed.
  final List<TextInputFormatter>? inputFormatters;

  /// Optional prefix icon to display at the start of the text field.
  final IconData? prefixIcon;

  /// Optional suffix icon to display at the end of the text field.
  final IconData? suffixIcon;

  /// Optional custom suffix widget to display at the end (e.g., password toggle).
  final Widget? suffix;

  /// Whether the text field should be read-only.
  final bool readOnly;

  /// Callback when the text field is tapped.
  final VoidCallback? onTap;

  const UiTextField({
    super.key,
    this.label,
    this.hintText,
    this.onChanged,
    this.controller,
    this.keyboardType,
    this.maxLines = 1,
    this.obscureText = false,
    this.textInputAction,
    this.onSubmitted,
    this.autofocus = false,
    this.inputFormatters,
    this.prefixIcon,
    this.suffixIcon,
    this.suffix,
    this.readOnly = false,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        if (label != null) ...<Widget>[
          Text(label!, style: UiTypography.body4m.textSecondary),
          const SizedBox(height: UiConstants.space1),
        ],
        TextField(
          controller: controller,
          onChanged: onChanged,
          keyboardType: keyboardType,
          maxLines: maxLines,
          obscureText: obscureText,
          textInputAction: textInputAction,
          onSubmitted: onSubmitted,
          autofocus: autofocus,
          inputFormatters: inputFormatters,
          readOnly: readOnly,
          onTap: onTap,
          style: UiTypography.body1r.textPrimary,
          decoration: InputDecoration(
            hintText: hintText,
            prefixIcon: prefixIcon != null
                ? Icon(prefixIcon, size: 20, color: UiColors.iconSecondary)
                : null,
            suffixIcon: suffixIcon != null
                ? Icon(suffixIcon, size: 20, color: UiColors.iconSecondary)
                : suffix,
          ),
        ),
      ],
    );
  }
}
