import 'package:flutter/material.dart';

import '../utils/nir_utils.dart';
import 'custom_app_text_field.dart';

/// Champ de saisie dédié au NIR (Numéro d'Inscription au Répertoire – 15 caractères).
/// Format affiché : 1 12 34 56 789 012 - 34 ou 1 12 34 2A 789 012 - 34 pour la Corse.
/// La valeur envoyée au [controller] est formatée ; utiliser [normalizeNir](controller.text) à la soumission.
class NirTextField extends StatelessWidget {
  final TextEditingController controller;
  final FocusNode? focusNode;
  /// Si non null, impose l’ordre Tab parmi les descendants (ex. après « Pays de naissance »).
  final double? focusTraversalOrder;
  final String labelText;
  final String hintText;
  final String? Function(String?)? validator;
  final double fieldWidth;
  final double fieldHeight;
  final double labelFontSize;
  final double inputFontSize;
  final bool enabled;
  final bool readOnly;
  final CustomAppTextFieldStyle style;
  final double labelFieldSpacing;

  const NirTextField({
    super.key,
    required this.controller,
    this.focusNode,
    this.focusTraversalOrder,
    this.labelText = 'N° Sécurité Sociale (NIR)',
    this.hintText = '13 chiffres + clé',
    this.validator,
    this.fieldWidth = double.infinity,
    this.fieldHeight = 53.0,
    this.labelFontSize = 18.0,
    this.inputFontSize = 18.0,
    this.enabled = true,
    this.readOnly = false,
    this.style = CustomAppTextFieldStyle.beige,
    this.labelFieldSpacing = 6.0,
  });

  @override
  Widget build(BuildContext context) {
    final field = CustomAppTextField(
      controller: controller,
      focusNode: focusNode,
      labelText: labelText,
      hintText: hintText,
      fieldWidth: fieldWidth,
      fieldHeight: fieldHeight,
      labelFontSize: labelFontSize,
      inputFontSize: inputFontSize,
      keyboardType: TextInputType.text,
      validator: validator ?? validateNir,
      inputFormatters: [NirInputFormatter()],
      enabled: enabled,
      readOnly: readOnly,
      style: style,
      labelFieldSpacing: labelFieldSpacing,
    );
    final o = focusTraversalOrder;
    if (o != null) {
      return FocusTraversalOrder(
        order: NumericFocusOrder(o),
        child: field,
      );
    }
    return field;
  }
}
