// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
// add_edit_location_doctor.dart
//
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:nokken/src/core/ui/widgets/shared_widgets.dart';
import 'package:nokken/src/core/services/database/medical_providers_repository.dart';
import 'package:nokken/src/core/ui/widgets/autocomplete_text_field.dart';

/// Location and doctor fields with autocomplete functionality
class LocationDoctorFields extends ConsumerWidget {
  final TextEditingController locationController;
  final TextEditingController doctorController;

  const LocationDoctorFields({
    super.key,
    required this.locationController,
    required this.doctorController,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // Watch the providers for location and doctor suggestions
    final locationsAsync = ref.watch(locationsProvider);
    final doctorsAsync = ref.watch(doctorsProvider);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Location field
        locationsAsync.when(
          data: (locations) {
            return AutocompleteTextField(
              controller: locationController,
              labelText: 'Location',
              hintText: 'Enter appointment location',
              options: locations,
            );
          },
          error: (_, __) {
            // On error, fall back to standard text field
            return TextFormField(
              controller: locationController,
              decoration: InputDecoration(
                labelText: 'Location',
                hintText: 'Enter appointment location',
              ),
            );
          },
          loading: () {
            // While loading, show autocomplete with loading state
            return AutocompleteTextField(
              controller: locationController,
              labelText: 'Location',
              hintText: 'Enter appointment location',
              options: const [],
              isLoading: true,
            );
          },
        ),
        SharedWidgets.verticalSpace(),

        // Doctor field
        doctorsAsync.when(
          data: (doctors) {
            return AutocompleteTextField(
              controller: doctorController,
              labelText: 'Healthcare Provider',
              hintText: 'Enter doctor or provider name',
              options: doctors,
            );
          },
          error: (_, __) {
            // On error, fall back to standard text field
            return TextFormField(
              controller: doctorController,
              decoration: InputDecoration(
                labelText: 'Healthcare Provider',
                hintText: 'Enter doctor or provider name',
              ),
            );
          },
          loading: () {
            // While loading, show autocomplete with loading state
            return AutocompleteTextField(
              controller: doctorController,
              labelText: 'Healthcare Provider',
              hintText: 'Enter doctor or provider name',
              options: const [],
              isLoading: true,
            );
          },
        ),
      ],
    );
  }
}
