import 'package:flutter/material.dart';
import 'package:on_field_work/helpers/utils/base_bottom_sheet.dart';

Future<String?> showCommentBottomSheet(BuildContext context, String actionText) async {
  final commentController = TextEditingController();
  String? errorText;

  return showModalBottomSheet<String>(
    context: context,
    isScrollControlled: true,
    backgroundColor: Colors.transparent,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
    ),
    builder: (context) {
      return StatefulBuilder(
        builder: (context, setModalState) {
          void submit() {
            final comment = commentController.text.trim();
            if (comment.isEmpty) {
              setModalState(() => errorText = 'Comment cannot be empty.');
              return;
            }
            Navigator.of(context).pop(comment);
          }

          return Padding(
            padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
            child: BaseBottomSheet(
              title: 'Add Comment for ${_capitalizeFirstLetter(actionText)}',
              onCancel: () => Navigator.of(context).pop(),
              onSubmit: submit,
              isSubmitting: false,
              submitText: 'Submit',
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  TextField(
                    controller: commentController,
                    maxLines: 4,
                    decoration: InputDecoration(
                      hintText: 'Type your comment here...',
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                      filled: true,
                      fillColor: Colors.grey.shade100,
                      errorText: errorText,
                    ),
                    onChanged: (_) {
                      if (errorText != null) {
                        setModalState(() => errorText = null);
                      }
                    },
                  ),
                ],
              ),
            ),
          );
        },
      );
    },
  );
}

String _capitalizeFirstLetter(String text) =>
    text.isEmpty ? text : text[0].toUpperCase() + text.substring(1);
