import 'package:get/get.dart'; import 'package:marco/helpers/services/api_service.dart'; import 'package:marco/helpers/services/app_logger.dart'; import 'package:marco/helpers/widgets/my_snackbar.dart'; import 'package:marco/controller/directory/directory_controller.dart'; class AddCommentController extends GetxController { final String contactId; AddCommentController({required this.contactId}); final RxString note = ''.obs; final RxBool isSubmitting = false.obs; Future submitComment() async { if (note.value.trim().isEmpty) { showAppSnackbar( title: "Validation", message: "Comment cannot be empty.", type: SnackbarType.warning, ); return; } isSubmitting.value = true; try { logSafe("Submitting comment for contactId: $contactId"); final success = await ApiService.addContactComment( note.value.trim(), contactId, ); if (success) { logSafe("Comment added successfully."); // Get the directory controller final directoryController = Get.find(); // Fetch latest comments for the contact to refresh UI await directoryController.fetchCommentsForContact(contactId); Get.back(result: true); showAppSnackbar( title: "Success", message: "Comment added successfully.", type: SnackbarType.success, ); } else { logSafe("Comment submission failed", level: LogLevel.error); showAppSnackbar( title: "Error", message: "Failed to add comment.", type: SnackbarType.error, ); } } catch (e) { logSafe("Error while submitting comment: $e", level: LogLevel.error); showAppSnackbar( title: "Error", message: "Something went wrong.", type: SnackbarType.error, ); } finally { isSubmitting.value = false; } } void updateNote(String value) { note.value = value; logSafe("Note updated: ${value.trim()}"); } }