import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_field_work/controller/directory/add_comment_controller.dart';
import 'package:on_field_work/helpers/utils/base_bottom_sheet.dart';

class AddCommentBottomSheet extends StatefulWidget {
  final String contactId;

  const AddCommentBottomSheet({super.key, required this.contactId});

  @override
  State<AddCommentBottomSheet> createState() => _AddCommentBottomSheetState();
}

class _AddCommentBottomSheetState extends State<AddCommentBottomSheet> {
  late final AddCommentController controller;
  final TextEditingController textController = TextEditingController();
  bool isSubmitting = false;

  @override
  void initState() {
    super.initState();
    controller = Get.put(AddCommentController(contactId: widget.contactId));
  }

  @override
  void dispose() {
    textController.dispose();
    super.dispose();
  }

  Future<void> handleSubmit() async {
    final noteText = textController.text.trim();
    if (noteText.isEmpty) return;

    setState(() {
      isSubmitting = true;
    });

    controller.updateNote(noteText);
    await controller.submitComment();

    if (mounted) {
      setState(() {
        isSubmitting = false;
      });
      Get.back(result: true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return BaseBottomSheet(
      title: "Add Note",
      onCancel: () => Get.back(),
      onSubmit: handleSubmit,
      isSubmitting: isSubmitting,
      child: TextField(
        controller: textController,
        maxLines: null,
        minLines: 5,
        decoration: InputDecoration(
          hintText: "Enter your note here...",
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(12),
          ),
          contentPadding: const EdgeInsets.all(12),
        ),
      ),
    );
  }
}
