import 'package:flutter/material.dart';
import 'base_field.dart';

class RadioField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;
  final List<String> options; // e.g., ['yes','no']

  RadioField({
    required this.fieldKey,
    required this.label,
    this.hint = '',
    this.isRequired = false,
    required this.options,
  });

  @override
  String? Function(String?)? get validator => (value) {
        if (isRequired && (value == null || value.isEmpty)) {
          return '$label is required';
        }
        if (value != null && value.isNotEmpty && !options.contains(value)) {
          return 'Invalid selection';
        }
        return null;
      };

  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    final String current = controller.text;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            color: colorScheme.onSurface,
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 6),
        Wrap(
          spacing: 12,
          children: options.map((opt) {
            return ChoiceChip(
              label: Text(opt),
              selected: current == opt,
              selectedColor: colorScheme.primary.withOpacity(0.2),
              onSelected: (_) {
                controller.text = opt;
                if (onChanged != null) onChanged();
              },
            );
          }).toList(),
        ),
      ],
    );
  }
}
