// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
// add_edit_medical_providers.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/ui/widgets/autocomplete_text_field.dart';
import 'package:nokken/src/core/services/database/medical_providers_repository.dart';

/// Doctor and Pharmacy Section
class MedicalProvidersSection extends ConsumerWidget {
  final TextEditingController doctorController;
  final TextEditingController pharmacyController;

  const MedicalProvidersSection({
    super.key,
    required this.doctorController,
    required this.pharmacyController,
  });

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

    return SharedWidgets.basicCard(
      context: context,
      title: 'Medical Providers',
      children: [
        // Doctor field
        doctorsAsync.when(
          data: (doctors) {
            return AutocompleteTextField(
              controller: doctorController,
              labelText: 'Doctor',
              hintText: 'Enter healthcare provider name',
              options: doctors,
            );
          },
          error: (_, __) {
            // On error, fall back to standard text field
            return TextFormField(
              controller: doctorController,
              decoration: InputDecoration(
                labelText: 'Doctor',
                hintText: 'Enter healthcare provider name',
              ),
            );
          },
          loading: () {
            // While loading, show autocomplete with loading state
            return AutocompleteTextField(
              controller: doctorController,
              labelText: 'Doctor',
              hintText: 'Enter healthcare provider name',
              options: const [],
              isLoading: true,
            );
          },
        ),
        SharedWidgets.verticalSpace(),

        // Pharmacy field
        pharmaciesAsync.when(
          data: (pharmacies) {
            return AutocompleteTextField(
              controller: pharmacyController,
              labelText: 'Pharmacy',
              hintText: 'Enter pharmacy name',
              options: pharmacies,
            );
          },
          error: (_, __) {
            // On error, fall back to standard text field
            return TextFormField(
              controller: pharmacyController,
              decoration: InputDecoration(
                labelText: 'Pharmacy',
                hintText: 'Enter pharmacy name',
              ),
            );
          },
          loading: () {
            // While loading, show autocomplete with loading state
            return AutocompleteTextField(
              controller: pharmacyController,
              labelText: 'Pharmacy',
              hintText: 'Enter pharmacy name',
              options: const [],
              isLoading: true,
            );
          },
        ),
      ],
    );
  }
}
