import 'dart:convert';
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:supplier_new/common/settings.dart';
import 'package:supplier_new/resources/source_loctaions_model.dart';
import 'package:supplier_new/resources/sourcelocation_details.dart';
import '../common/keys.dart';
import '../google_maps_place_picker_mb/src/models/pick_result.dart';
import '../google_maps_place_picker_mb/src/place_picker.dart';
import 'package:supplier_new/google_maps_place_picker_mb/google_maps_place_picker.dart';
import 'dart:io' show File, Platform;
import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'package:location/location.dart' as locationmap;

class FirstCharUppercaseFormatter extends TextInputFormatter {
  const FirstCharUppercaseFormatter();

  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue,
      TextEditingValue newValue,
      ) {
    final text = newValue.text;
    if (text.isEmpty) return newValue;

    // Find first non-space char
    final i = text.indexOf(RegExp(r'\S'));
    if (i == -1) return newValue;

    final first = text[i];
    final upper = first.toUpperCase();
    if (first == upper) return newValue;

    final newText = text.replaceRange(i, i + 1, upper);

    return newValue.copyWith(
      text: newText,
      selection: newValue.selection,
      composing: TextRange.empty,
    );
  }
}


class ResourcesSourceScreen extends StatefulWidget {
  const ResourcesSourceScreen({super.key});

  @override
  State<ResourcesSourceScreen> createState() => _ResourcesSourceScreenState();
}

PickResult? selectedPlace;

bool _mapsInitialized = false;
final String _mapsRenderer = "latest";

var kInitialPosition = const LatLng(15.462477, 78.717401);

locationmap.Location location = locationmap.Location();

final GoogleMapsFlutterPlatform mapsImplementation =
    GoogleMapsFlutterPlatform.instance;

void initRenderer() {
  if (_mapsInitialized) return;
  if (mapsImplementation is GoogleMapsFlutterAndroid) {
    switch (_mapsRenderer) {
      case "legacy":
        (mapsImplementation as GoogleMapsFlutterAndroid)
            .initializeWithRenderer(AndroidMapRenderer.legacy);
        break;
      case "latest":
        (mapsImplementation as GoogleMapsFlutterAndroid)
            .initializeWithRenderer(AndroidMapRenderer.latest);
        break;
    }
  }
  // setState(() {
  //   _mapsInitialized = true;
  // });
}


class _ResourcesSourceScreenState extends State<ResourcesSourceScreen> {
  int selectedTab = 2;
  String search = '';
  bool isLoading = false;
  String selectedWaterFilter = "all";
  List<SourceLocationsModel> sourceLocationsList = [];

  // ---------- Form state (bottom sheet) ----------
  final _formKey = GlobalKey<FormState>();
  final _locationNameController = TextEditingController();
  final _mobileCtrl = TextEditingController();

  bool addBusinessAsSource = false; // toggle if you want to hide map input
  String? selectedWaterType;
  final List<String> waterTypes = const ['Drinking water', 'Bore water', 'Both'];

  double? lat;
  double? lng;
  String? address;
  int drinkingCount = 0;
  int boreCount = 0;
  int bothCount = 0;

  String? _required(String? v, {String field = "This field"}) {
    if (v == null || v.trim().isEmpty) return "$field is required";
    return null;
  }

  String? _validatePhone(String? v, {String label = "Phone"}) {
    if (v == null || v.trim().isEmpty) return "$label is required";
    if (v.trim().length != 10) return "$label must be 10 digits";
    return null;
  }

  String? selectedFilter;
  String selectedSort = "Name";

  List<String> filterOptions = [
    "All",
    "Drinking water",
    "Bore water",
    "Both"
  ];

  List<String> sortOptions = [
    "Name",
    "Location"
  ];

  void _showFilterMenu(BuildContext context, Offset position) async {

    await showMenu<String>(

      context: context,

      color: Colors.white,

      position: RelativeRect.fromLTRB(
          position.dx,
          position.dy,
          position.dx + 200,
          position.dy + 200),

      items: [

        PopupMenuItem<String>(
          enabled:false,
          child: Text(
            "Filter",
            style: fontTextStyle(
                12,
                Color(0XFF2D2E30),
                FontWeight.w600),
          ),
        ),

        ...filterOptions.map((option){

          return PopupMenuItem<String>(

            child: RadioListTile<String>(

              value: option,

              groupValue:
              selectedFilter ?? "All",

              activeColor:
              Color(0XFF1D7AFC),

              onChanged:(value){

                setState(() {

                  selectedFilter =
                  value=="All"
                      ? null
                      : value;

                });

                Navigator.pop(context);

              },

              title: Text(
                option,
                style: fontTextStyle(
                    12,
                    Color(0XFF2D2E30),
                    FontWeight.w400),
              ),

              dense:true,

              visualDensity:
              VisualDensity(
                  horizontal:-4,
                  vertical:-4),

            ),

          );

        }).toList()

      ],

    );

  }

  void _showSortMenu(BuildContext context, Offset position) async {

    await showMenu<String>(

      context: context,

      color: Colors.white,

      position: RelativeRect.fromLTRB(
          position.dx,
          position.dy,
          position.dx + 200,
          position.dy + 200),

      items: [

        PopupMenuItem<String>(
          enabled:false,
          child: Text(
            "Sort",
            style: fontTextStyle(
                12,
                Color(0XFF2D2E30),
                FontWeight.w600),
          ),
        ),

        ...sortOptions.map((option){

          return PopupMenuItem<String>(

            child: RadioListTile<String>(

              value: option,

              groupValue: selectedSort,

              activeColor:
              Color(0XFF1D7AFC),

              onChanged:(value){

                setState(() {

                  selectedSort =
                      value.toString();

                });

                Navigator.pop(context);

              },

              title: Text(
                option,
                style: fontTextStyle(
                    12,
                    Color(0XFF2D2E30),
                    FontWeight.w400),
              ),

              dense:true,

              visualDensity:
              VisualDensity(
                  horizontal:-4,
                  vertical:-4),

            ),

          );

        }).toList()

      ],

    );

  }

  @override
  void initState() {
    super.initState();
    _fetchSources();
  }

  @override
  void dispose() {
    _locationNameController.dispose();
    _mobileCtrl.dispose();
    super.dispose();
  }

  void _resetForm() {
    _formKey.currentState?.reset();
    _locationNameController.clear();
    _mobileCtrl.clear();
    selectedWaterType = null;
    // keep address/lat/lng as last-picked unless you want to clear:
    // address = null; lat = null; lng = null;
  }

  Future<void> _fetchSources() async {
    setState(() => isLoading = true);
    try {
      final response = await AppSettings.getSourceLoctaions();
      final data = (jsonDecode(response)['data'] as List)
          .map((e) => SourceLocationsModel.fromJson(e))
          .toList();

      int drinking = 0;
      int bore = 0;
      int both = 0;

      for (final s in data) {
        switch ((s.water_type ?? '').toLowerCase()) {
          case 'drinking water':
            drinking++;
            break;
          case 'bore water':
            bore++;
            break;
          case 'both':
            both++;
            break;
        }
      }

      if (!mounted) return;
      setState(() {
        sourceLocationsList = data;
        drinkingCount = drinking;
        boreCount = bore;
        bothCount = both;
        isLoading = false;
      });
    } catch (e) {
      debugPrint("⚠️ Error fetching source locations: $e");
      setState(() => isLoading = false);
    }
  }

  Future<void> _addSourceLocation() async {
    final ok = _formKey.currentState?.validate() ?? false;

    if (addBusinessAsSource) {
      // Use supplier's address as the source location
      address = AppSettings.userAddress;
      lat = AppSettings.supplierLatitude;
      lng = AppSettings.supplierLongitude;
      setState(() {});
    }

    if (!ok || selectedWaterType == null || selectedWaterType!.isEmpty) {
      if (selectedWaterType == null) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text("Please select Water Type")),
        );
      }
      setState(() {}); // ensure errors render
      return;
    }

    final payload = <String, dynamic>{
      "location_name": _locationNameController.text.trim(),
      "phone": _mobileCtrl.text.trim(),
      "water_type": selectedWaterType,
      "status": 'active', // or whatever string your backend expects
      "address": address,
      "city": '',
      "state": '',
      "zip": '',
      "latitude": lat,
      "longitude": lng,
    };

    try {
      final bool ok = await AppSettings.addSourceLocations(payload);
      if (!mounted) return;

      if (ok) {
        AppSettings.longSuccessToast("Source location added successfully");
        Navigator.pop(context, true); // close the sheet
        _resetForm();
        _fetchSources(); // refresh list
      } else {
        AppSettings.longFailedToast("Source location creation failed");
      }
    } catch (e) {
      debugPrint("⚠️ addSourceLocations error: $e");
      if (!mounted) return;
      AppSettings.longFailedToast("Something went wrong");
    }
  }

  Future<void> _openSourceFormSheet(BuildContext context)async {
    await showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    backgroundColor: Colors.transparent,
      builder: (context) {
        final kbInset = MediaQuery.of(context).viewInsets.bottom;
        return FractionallySizedBox(
          heightFactor: 0.75, // fixed height
          child: Container(
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
            ),
            child: Padding(
              padding: EdgeInsets.fromLTRB(20, 16, 20, 20 + kbInset),
              child: Form(
                key: _formKey,
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Row(
                        children: [
                          Expanded(
                            child: Center(
                              child: Container(
                                width: 86,
                                height: 4,
                                margin: const EdgeInsets.only(bottom: 12),
                                decoration: BoxDecoration(
                                  color: const Color(0xFFE0E0E0),
                                  borderRadius: BorderRadius.circular(2),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),

                      _LabeledField(
                        label: "Location Name *",
                        child: TextFormField(
                          controller: _locationNameController,
                          validator: (v) => _required(v, field: "Location Name"),
                          textCapitalization: TextCapitalization.none,
                          inputFormatters: const [
                            FirstCharUppercaseFormatter(), // << live first-letter caps
                          ],
                          decoration: InputDecoration(
                            hintText: "Location Name",
                            hintStyle: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
                            border: const OutlineInputBorder(),
                            isDense: true,
                          ),
                          textInputAction: TextInputAction.next,
                        ),
                      ),

                      _LabeledField(
                        label: "Phone Number *",
                        child: TextFormField(
                          controller: _mobileCtrl,
                          keyboardType: TextInputType.phone,
                          autovalidateMode: AutovalidateMode.onUserInteraction,
                          inputFormatters: [
                            FilteringTextInputFormatter.digitsOnly,
                            LengthLimitingTextInputFormatter(10),

                            // Restrict first digit 6-9
                            TextInputFormatter.withFunction(
                                  (oldValue, newValue) {

                                if (newValue.text.isEmpty) {
                                  return newValue;
                                }

                                // First digit must be 6-9
                                if (!RegExp(r'^[6-9]').hasMatch(newValue.text)) {
                                  return oldValue;
                                }

                                return newValue;
                              },
                            ),
                          ],

                          validator: (value) {

                            if (value == null || value.isEmpty) {
                              return "Phone Number required";
                            }

                            if (!RegExp(r'^[6-9]').hasMatch(value)) {
                              return "Enter digits starting 6,7,8,9";
                            }

                            if (value.length != 10) {
                              return "Enter valid 10 digit number";
                            }

                            return null;
                          },

                          decoration: InputDecoration(
                            hintText: "Mobile Number",
                            hintStyle: fontTextStyle(
                                14, const Color(0xFF939495), FontWeight.w400),
                            border: const OutlineInputBorder(),
                            isDense: true,
                          ),

                          textInputAction: TextInputAction.next,
                        ),
                      ),

                      Visibility(
                        visible: !addBusinessAsSource,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            SizedBox(
                              width: double.infinity,
                              child: OutlinedButton.icon(
                                onPressed: () {
                                  // Your PlacePicker flow goes here (kept commented for reference)
                                  location.serviceEnabled().then((value) {
                                    if (value) {
                                      initRenderer();
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) {
                                            return PlacePicker(
                                              resizeToAvoidBottomInset: false,
                                              hintText: "Find a place ...",
                                              searchingText: "Please wait ...",
                                              selectText: "Select place",
                                              outsideOfPickAreaText: "Place not in area",
                                              initialPosition: kInitialPosition,
                                              useCurrentLocation: true,
                                              selectInitialPosition: true,
                                              usePinPointingSearch: true,
                                              usePlaceDetailSearch: true,
                                              zoomGesturesEnabled: true,
                                              zoomControlsEnabled: true,
                                              onMapCreated: (GoogleMapController controller) {},
                                              onPlacePicked: (PickResult result) {
                                                setState(() {
                                                  selectedPlace = result;
                                                  lat = selectedPlace!.geometry!.location.lat;
                                                  lng = selectedPlace!.geometry!.location.lng;
                                                  if (selectedPlace!.types!.length == 1) {
                                                    address = selectedPlace!.formattedAddress!;
                                                  } else {
                                                    address = '${selectedPlace!.name!}, ${selectedPlace!.formattedAddress!}';
                                                  }
                                                  Navigator.of(context).pop();
                                                });
                                              },
                                              onMapTypeChanged: (MapType mapType) {},
                                              apiKey: Platform.isAndroid ? APIKeys.androidApiKey : APIKeys.iosApiKey,
                                              forceAndroidLocationManager: true,
                                            );
                                          },
                                        ),
                                      );
                                    } else {
                                      showGeneralDialog(
                                        context: context,
                                        pageBuilder: (context, x, y) {
                                          return Scaffold(
                                            backgroundColor: Colors.grey.withOpacity(.5),
                                            body: Center(
                                              child: Container(
                                                width: double.infinity,
                                                height: 150,
                                                padding: const EdgeInsets.symmetric(horizontal: 20),
                                                child: Card(
                                                  child: Padding(
                                                    padding: const EdgeInsets.all(10.0),
                                                    child: Column(
                                                      mainAxisAlignment: MainAxisAlignment.center,
                                                      children: [
                                                        const Text(
                                                          "Please enable the location",
                                                          style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                                                        ),
                                                        const SizedBox(height: 20),
                                                        ElevatedButton(
                                                          onPressed: () {
                                                            Navigator.pop(context);
                                                          },
                                                          child: const Text("Cancel"),
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          );
                                        },
                                      );
                                    }
                                  });
                                },
                                icon: Image.asset('images/Add_icon.png', width: 16, height: 16),
                                label: Text(
                                  "Add Location on map",
                                  style: fontTextStyle(14, const Color(0xFF646566), FontWeight.w600),
                                ),
                              ),
                            ),
                            const SizedBox(height: 12),
                          ],
                        ),
                      ),

                      _LabeledField(
                        label: "Water Type *",
                        child: DropdownButtonFormField<String>(
                          value: selectedWaterType,
                          dropdownColor: Colors.white,
                          items: waterTypes
                              .map((w) => DropdownMenuItem(value: w, child: Text(w)))
                              .toList(),
                          onChanged: (v) => setState(() => selectedWaterType = v),
                          validator: (v) => v == null || v.isEmpty ? "Water Type is required" : null,
                          isExpanded: true,
                          alignment: Alignment.centerLeft,
                          hint: Text(
                            "Select Water Type",
                            style: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
                          ),
                          icon: Image.asset('images/downarrow.png', width: 16, height: 16),
                          decoration: const InputDecoration(
                            border: OutlineInputBorder(),
                            isDense: false,
                            contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                          ),
                        ),
                      ),

                      const SizedBox(height: 20),

                      // Actions
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          SizedBox(
                            width: double.infinity,
                            child: OutlinedButton.icon(
                              onPressed: _addSourceLocation,
                              icon: Image.asset('images/Add_icon.png', width: 16, height: 16),
                              label: Text(
                                "Add Location",
                                style: fontTextStyle(14, const Color(0xFF646566), FontWeight.w600),
                              ),
                            ),
                          ),
                          const SizedBox(height: 12),
                          SizedBox(
                            width: double.infinity,
                            child: ElevatedButton(
                              style: ElevatedButton.styleFrom(
                                backgroundColor: const Color(0xFF8270DB),
                                foregroundColor: Colors.white,
                                padding: const EdgeInsets.symmetric(vertical: 14),
                                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
                              ),
                              onPressed: _addSourceLocation,
                              child: Text(
                                "Save",
                                style: fontTextStyle(14, Colors.white, FontWeight.w600),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  // ---------- UI ----------
  @override
  Widget build(BuildContext context) {
    List<SourceLocationsModel> filtered =
    sourceLocationsList.where((it){

      final q = search.trim().toLowerCase();

      bool matchesSearch =
          q.isEmpty ||
              it.source_name.toLowerCase().contains(q) ||
              it.address.toLowerCase().contains(q);

      bool matchesFilter = true;

// Top metric filter
      if(selectedWaterFilter != "all"){

        matchesFilter =
            it.water_type.toLowerCase().trim() ==
                selectedWaterFilter;

      }

// Menu filter
      if(selectedFilter!=null){

        matchesFilter =
            it.water_type
                .toLowerCase()
                .trim() ==
                selectedFilter!
                    .toLowerCase();

      }

      return matchesSearch && matchesFilter;

    }).toList();

    if(selectedSort=="Name"){

      filtered.sort(
              (a,b)=>a.source_name
              .compareTo(b.source_name));

    }

    if(selectedSort=="Location"){

      filtered.sort(
              (a,b)=>a.address
              .compareTo(b.address));

    }

    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: [
          // Top card section
          Container(
            color: Colors.white,
            padding: const EdgeInsets.fromLTRB(16, 12, 16, 12),
            child: Column(
              children: [
                const SizedBox(height: 12),
                // Total Source Locations card
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(14),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12),
                    border: Border.all(color: const Color(0xFF939495)),
                  ),
                  child: Row(
                    children: [
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            width: 40,
                            height: 40,
                            decoration: const BoxDecoration(
                              color: Color(0xFFF6F0FF),
                              borderRadius: BorderRadius.all(Radius.circular(8)),
                            ),
                            child: Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Image.asset('images/drivers.png', fit: BoxFit.contain),
                            ),
                          ),
                          const SizedBox(height: 8),
                          Text(
                            'Total Source Locations',
                            style: fontTextStyle(12, const Color(0xFF2D2E30), FontWeight.w500),
                          ),
                        ],
                      ),
                      const Spacer(),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text(
                            sourceLocationsList.length.toString(),
                            style: fontTextStyle(24, const Color(0xFF0D3771), FontWeight.w500),
                          ),
                          const SizedBox(height: 6),
                          Text('no recent changes',
                              style: fontTextStyle(10, const Color(0xFF646566), FontWeight.w400)),
                        ],
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 12),
                IntrinsicHeight(
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [

                      Expanded(
                        child: GestureDetector(
                          onTap: (){
                            setState(() {
                              selectedWaterFilter = "drinking water";
                              selectedFilter = null;
                            });
                          },
                          child: SmallMetricBox(
                            title: 'Drinking water',
                            value: drinkingCount.toString(),
                          ),
                        ),
                      ),

                      const SizedBox(width: 8),

                      Expanded(
                        child: GestureDetector(
                          onTap: (){
                            setState(() {
                              selectedWaterFilter = "bore water";
                              selectedFilter = null;
                            });
                          },
                          child: SmallMetricBox(
                            title: 'Bore water',
                            value: boreCount.toString(),
                          ),
                        ),
                      ),

                      const SizedBox(width: 8),

                      Expanded(
                        child: GestureDetector(
                          onTap: (){
                            setState(() {
                              selectedWaterFilter = "both";
                              selectedFilter = null;
                            });
                          },
                          child: SmallMetricBox(
                            title: 'Both',
                            value: bothCount.toString(),
                          ),
                        ),
                      ),

                    ],
                  ),
                ),
              ],
            ),
          ),

          // List section
          Expanded(
            child: Container(
              color: const Color(0xFFF5F5F5),
              child: Padding(
                padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
                child: Column(
                  children: [
                    Row(
                      children: [
                        SizedBox(
                          width: 270,
                          child: Container(
                            padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                            decoration: BoxDecoration(
                              border: Border.all(color: const Color(0xFF939495), width: 0.5),
                              borderRadius: BorderRadius.circular(22),
                            ),
                            child: Row(
                              children: [
                                Image.asset('images/search.png', width: 20, height: 20),
                                const SizedBox(width: 8),
                                Expanded(
                                  child: TextField(
                                    decoration: InputDecoration(
                                      hintText: 'Search',
                                      hintStyle: fontTextStyle(12, const Color(0xFF939495), FontWeight.w400),
                                      border: InputBorder.none,
                                      isDense: true,
                                    ),
                                    onChanged: (v) => setState(() => search = v),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        const SizedBox(width: 16),
                        GestureDetector(

                          onTapDown:(details){

                            _showFilterMenu(
                                context,
                                details.globalPosition);

                          },

                          child: Image.asset(
                              'images/icon_tune.png',
                              width:24,
                              height:24),

                        ),
                        const SizedBox(width: 16),
                        GestureDetector(

                          onTapDown:(details){

                            _showSortMenu(
                                context,
                                details.globalPosition);

                          },

                          child: Image.asset(
                              'images/up_down_arrow.png',
                              width:24,
                              height:24),

                        ),
                      ],
                    ),
                    const SizedBox(height: 12),
                    Expanded(
                      child: isLoading
                          ? const Center(child: CircularProgressIndicator())
                          : (filtered.isEmpty
                          ? Center(
                        child: Padding(
                          padding: const EdgeInsets.symmetric(
                              vertical: 12),
                          child: Text(
                            'No Data Available',
                            style: fontTextStyle(
                                12,
                                const Color(0xFF939495),
                                FontWeight.w500),
                          ),
                        ),
                      )
                          : ListView.separated(
                        itemCount: filtered.length,
                        separatorBuilder: (_, __) => const SizedBox(height: 10),
                        itemBuilder: (context, idx) {
                          final it = filtered[idx];
                          return GestureDetector(
                            onTap: ()async{

                              final result = await Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) =>
                                      SourceDetailsScreen(
                                          sourceDetails: it),
                                ),
                              );
                              if (result == true) {
                                _fetchSources();
                              }
                            },
                            child: TankCard(
                              title: it.source_name,
                              subtitle: it.address,
                            ),
                          );
                        },
                      )),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _openSourceFormSheet(context),
        backgroundColor: const Color(0xFF000000),
        shape: const CircleBorder(),
        child: const Icon(Icons.add, color: Colors.white),
      ),
    );
  }
}

// ====== SmallMetricBox ======
class SmallMetricBox extends StatelessWidget {
  final String title;
  final String value;

  const SmallMetricBox({super.key, required this.title, required this.value});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: const Color(0xFF939495)),
        color: Colors.white,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: fontTextStyle(12, const Color(0xFF2D2E30), FontWeight.w500),
          ),
          Text(
            value,
            style: fontTextStyle(24, const Color(0xFF0D3771), FontWeight.w500),
          ),
        ],
      ),
    );
  }
}

// ====== TankCard ======
class TankCard extends StatelessWidget {
  final String title;
  final String subtitle;

  const TankCard({
    super.key,
    required this.title,
    required this.subtitle,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.grey.shade200),
      ),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, style: fontTextStyle(14, const Color(0xFF343637), FontWeight.w600)),
                const SizedBox(height: 6),
                Text(subtitle, style: fontTextStyle(10, const Color(0xFF515253), FontWeight.w600)),
              ],
            ),
          ),
          const SizedBox(width: 8),
          Container(
            padding: const EdgeInsets.all(8),
            decoration: const BoxDecoration(
              color: Color(0xFFF5F6F6),
              shape: BoxShape.circle,
            ),
            child: Image.asset("images/phone_icon.png", width: 20, height: 20),
          ),
        ],
      ),
    );
  }
}

// ====== Labeled Field Wrapper ======
class _LabeledField extends StatelessWidget {
  final String label;
  final Widget child;

  const _LabeledField({required this.label, required this.child});

  String _capFirstWord(String input) {
    if (input.isEmpty) return input;
    final i = input.indexOf(RegExp(r'\S'));
    if (i == -1) return input;
    return input.replaceRange(i, i + 1, input[i].toUpperCase());
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 14.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            _capFirstWord(label),
            style: fontTextStyle(12, const Color(0xFF515253), FontWeight.w600),
          ),
          const SizedBox(height: 6),
          child,
        ],
      ),
    );
  }
}
