import React from 'react';
import { getAllChargePointsForMapAction } from '@/modules/chargepointModule/list/actions/chargepoint.action';
import { MapClientWithFilters } from './sections/MapClientWithFilters';
import { FullScreenMapWrapper } from './sections/FullScreenMapWrapper';
import { ErrorState } from './components/MapError';


export default async function MapModule() {
  // Fetch data on the server
  let chargePoints: any[] = [];
  let error = null;

  try {
    const res = await getAllChargePointsForMapAction();
    chargePoints = res.success ? (res.data || []) : [];
  } catch (err) {
    console.error('Failed to load charge points:', err);
    error = 'Failed to load charge points';
  }

  // Get environment variables on the server
  const apiKey = process.env.GOOGLE_MAPS_API_KEY;
  const mapId = process.env.GOOGLE_MAPS_ID;

  // Check for missing environment variables
  if (!apiKey || !mapId) {
    error = 'Google Maps API configuration is missing';
  }

  return (
    <FullScreenMapWrapper>
      {error ? (
        <ErrorState message={error} />
      ) : (
        <div className="h-full w-full relative overflow-hidden">
          <MapClientWithFilters 
            initialStations={chargePoints}
            apiKey={apiKey!}
            mapId={mapId!}
          />
        </div>
      )}
    </FullScreenMapWrapper>
  );
}