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

class CheckboxField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;

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

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

  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    final bool checked = controller.text.toLowerCase() == 'true';
    return Row(
      children: [
        Checkbox(
          value: checked,
          onChanged: (value) {
            controller.text = (value ?? false).toString();
            if (onChanged != null) onChanged();
          },
        ),
        const SizedBox(width: 8),
        Expanded(
          child: Text(
            label,
            style: TextStyle(
              color: colorScheme.onSurface,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ],
    );
  }
}
