import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_field_work/helpers/widgets/my_spacing.dart';
import 'package:on_field_work/helpers/widgets/my_text.dart';
import 'package:on_field_work/helpers/utils/base_bottom_sheet.dart';
import 'package:on_field_work/model/employees/employee_model.dart';

class ReusableEmployeeSelectorBottomSheet extends StatelessWidget {
  final TextEditingController searchController;
  final RxList<EmployeeModel> searchResults;
  final RxBool isSearching;
  final void Function(String) onSearch;
  final void Function(EmployeeModel) onSelect;

  const ReusableEmployeeSelectorBottomSheet({
    super.key,
    required this.searchController,
    required this.searchResults,
    required this.isSearching,
    required this.onSearch,
    required this.onSelect,
  });

  @override
  Widget build(BuildContext context) {
    return BaseBottomSheet(
      title: "Search Employee",
      onCancel: () => Get.back(),
      onSubmit: () {},
      showButtons: false,
      child: Obx(() {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextField(
              controller: searchController,
              decoration: InputDecoration(
                hintText: "Search by name, email...",
                prefixIcon: const Icon(Icons.search),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                contentPadding:
                    const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
              ),
              onChanged: onSearch,
            ),
            MySpacing.height(12),
            SizedBox(
              height: 400,
              child: isSearching.value
                  ? const Center(child: CircularProgressIndicator())
                  : searchResults.isEmpty
                      ? Center(
                          child: MyText.bodyMedium(
                            "No employees found.",
                            fontWeight: 500,
                          ),
                        )
                      : ListView.builder(
                          itemCount: searchResults.length,
                          itemBuilder: (_, index) {
                            final emp = searchResults[index];
                            final fullName =
                                '${emp.firstName} ${emp.lastName}'.trim();
                            return ListTile(
                              title: MyText.bodyLarge(
                                fullName.isNotEmpty ? fullName : "Unnamed",
                                fontWeight: 600,
                              ),
                              onTap: () {
                                onSelect(emp);
                                Get.back();
                              },
                            );
                          },
                        ),
            ),
          ],
        );
      }),
    );
  }
}
