import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:p_tits_pas/widgets/custom_app_text_field.dart';

class ModularFormField {
  final String label;
  final String hint;
  final TextEditingController controller;
  final TextInputType? keyboardType;
  final bool isPassword;
  final String? Function(String?)? validator;
  final bool isRequired;
  final int flex;

  ModularFormField({
    required this.label,
    required this.hint,
    required this.controller,
    this.keyboardType,
    this.isPassword = false,
    this.validator,
    this.isRequired = false,
    this.flex = 1,
  });
}

class ModularForm extends StatelessWidget {
  final List<List<ModularFormField>> fieldGroups;
  final GlobalKey<FormState> formKey;
  final double? width;
  final EdgeInsets padding;
  final String? title;
  final VoidCallback? onSubmit;
  final String submitLabel;

  const ModularForm({
    super.key,
    required this.fieldGroups,
    required this.formKey,
    this.width,
    this.padding = const EdgeInsets.all(20),
    this.title,
    this.onSubmit,
    this.submitLabel = "Suivant",
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width ?? MediaQuery.of(context).size.width * 0.6,
      padding: padding,
      child: Form(
        key: formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (title != null) ...[
              Text(
                title!,
                style: GoogleFonts.merienda(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 20),
            ],
            ...fieldGroups.map((group) {
              return Column(
                children: [
                  Row(
                    children: group.asMap().entries.map((entry) {
                      final index = entry.key;
                      final field = entry.value;
                      
                      return [
                        Expanded(
                          flex: field.flex,
                          child: CustomAppTextField(
                            controller: field.controller,
                            labelText: field.label,
                            hintText: field.hint,
                            obscureText: field.isPassword,
                            keyboardType: field.keyboardType ?? TextInputType.text,
                            validator: field.validator,
                            style: CustomAppTextFieldStyle.beige,
                            fieldWidth: double.infinity, // CORRECTION PRINCIPALE
                          ),
                        ),
                        // Ajouter un espaceur entre les champs (sauf pour le dernier)
                        if (index < group.length - 1)
                          const Expanded(
                            flex: 1,
                            child: SizedBox(), // Espacement de 4% comme dans l'original
                          ),
                      ];
                    }).expand((element) => element).toList(),
                  ),
                  const SizedBox(height: 20),
                ],
              );
            }).toList(),
            if (onSubmit != null)
              Center(
                child: ElevatedButton(
                  onPressed: onSubmit,
                  child: Text(submitLabel),
                ),
              ),
          ],
        ),
      ),
    );
  }
}