import 'package:syncrow_web/pages/analytics/models/occupancy_heat_map_model.dart';
import 'package:syncrow_web/pages/analytics/params/get_occupancy_heat_map_param.dart';
import 'package:syncrow_web/pages/analytics/services/occupancy_heat_map/occupancy_heat_map_service.dart';
import 'package:syncrow_web/services/api/http_service.dart';

final class RemoteOccupancyHeatMapService implements OccupancyHeatMapService {
  const RemoteOccupancyHeatMapService(this._httpService);

  final HTTPService _httpService;

  @override
  Future<List<OccupancyHeatMapModel>> load(GetOccupancyHeatMapParam param) async {
    try {
      final response = await _httpService.get(
        path: '/occupancy/heat-map/space/${param.spaceUuid}',
        showServerMessage: true,
        queryParameters: param.toJson(),
        expectedResponseModel: (response) {
          final json = response as Map<String, dynamic>;
          final dailyData = json['data'] as List<dynamic>? ?? <dynamic>[];

          final result = dailyData.map(
            (json) => OccupancyHeatMapModel.fromJson(json as Map<String, dynamic>),
          );

          return result.toList();
        },
      );

      return response;
    } catch (e) {
      throw Exception('Failed to load total energy consumption:');
    }
  }
}
