import 'package:flutter/material.dart';
import 'package:design_system/design_system.dart';
import 'package:krow_domain/krow_domain.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

/// A widget that displays the shift location on an interactive Google Map.
class ShiftLocationMap extends StatefulWidget {
  /// The shift entity containing location and coordinates.
  final Shift shift;
  
  /// The height of the map widget.
  final double height;
  
  /// The border radius for the map container.
  final double borderRadius;

  /// Creates a [ShiftLocationMap].
  const ShiftLocationMap({
    super.key,
    required this.shift,
    this.height = 120,
    this.borderRadius = 8,
  });

  @override
  State<ShiftLocationMap> createState() => _ShiftLocationMapState();
}

class _ShiftLocationMapState extends State<ShiftLocationMap> {
  late final CameraPosition _initialPosition;
  final Set<Marker> _markers = {};

  @override
  void initState() {
    super.initState();
    
    // Default to a fallback coordinate if latitude/longitude are null.
    // In a real app, you might want to geocode the address if coordinates are missing.
    final double lat = widget.shift.latitude ?? 0.0;
    final double lng = widget.shift.longitude ?? 0.0;
    
    final LatLng position = LatLng(lat, lng);
    
    _initialPosition = CameraPosition(
      target: position,
      zoom: 15,
    );

    _markers.add(
      Marker(
        markerId: MarkerId(widget.shift.id),
        position: position,
        infoWindow: InfoWindow(
          title: widget.shift.location,
          snippet: widget.shift.locationAddress,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // If coordinates are missing, we show a placeholder.
    if (widget.shift.latitude == null || widget.shift.longitude == null) {
      return _buildPlaceholder(context, "Coordinates unavailable");
    }

    return Container(
      height: widget.height * 1.25, // Slightly taller to accommodate map controls
      width: double.infinity,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(widget.borderRadius),
        border: Border.all(color: UiColors.border),
      ),
      clipBehavior: Clip.antiAlias,
      child: GoogleMap(
        initialCameraPosition: _initialPosition,
        markers: _markers,
        liteModeEnabled: true, // Optimized for static-like display in details page
        scrollGesturesEnabled: false,
        zoomGesturesEnabled: true,
        tiltGesturesEnabled: false,
        rotateGesturesEnabled: false,
        myLocationButtonEnabled: false,
        mapToolbarEnabled: false,
        compassEnabled: false,
      ),
    );
  }

  Widget _buildPlaceholder(BuildContext context, String message) {
    return Container(
      height: widget.height,
      width: double.infinity,
      decoration: BoxDecoration(
        color: UiColors.bgThird,
        borderRadius: BorderRadius.circular(widget.borderRadius),
        border: Border.all(color: UiColors.border),
      ),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(
              UiIcons.mapPin,
              size: 32,
              color: UiColors.primary,
            ),
            if (message.isNotEmpty) ...[
              const SizedBox(height: UiConstants.space2),
              Text(
                message,
                style: UiTypography.footnote2r.textSecondary,
              ),
            ],
          ],
        ),
      ),
    );
  }
}
