import '../core/networking/api_client.dart';
import '../models/content_models.dart';
import 'discovery_repository.dart';
class HttpDiscoveryRepository implements DiscoveryRepository {
const HttpDiscoveryRepository(this.client);
final ApiClient client;
@override
Future<DiscoveryResponse> discover({
required List<String> types,
String? search,
double? latitude,
double? longitude,
double? radiusKm = 10,
int page = 1,
int perPage = 40,
String? category,
String? region,
String? status,
Map<String, String> filters = const {},
String sort = 'auto',
double? minLatitude,
double? maxLatitude,
double? minLongitude,
double? maxLongitude,
}) async {
final query = <String, String>{
'types': types.join(','),
'page': '$page',
'per_page': '$perPage',
'sort': sort,
};
if (radiusKm != null) {
query['radius_km'] = _radiusValue(radiusKm);
}
if (search != null && search.trim().isNotEmpty) {
query['search'] = search.trim();
}
if (latitude != null && longitude != null) {
query['lat'] = '$latitude';
query['lng'] = '$longitude';
}
if (category != null && category.trim().isNotEmpty) {
query['category'] = category.trim();
}
if (region != null && region.trim().isNotEmpty) {
query['region'] = region.trim();
}
if (status != null && status.trim().isNotEmpty) {
query['status'] = status.trim();
}
for (final entry in filters.entries) {
if (entry.key.trim().isEmpty || entry.value.trim().isEmpty) continue;
query[entry.key] = entry.value.trim();
}
final bounds = <String, double?>{
'min_lat': minLatitude,
'max_lat': maxLatitude,
'min_lng': minLongitude,
'max_lng': maxLongitude,
};
if (bounds.values.every((value) => value != null)) {
for (final entry in bounds.entries) {
query[entry.key] = '${entry.value}';
}
}
return DiscoveryResponse.fromJson(
await client.getPublicJson('/discover', queryParameters: query),
);
}
static String _radiusValue(double value) => value.truncateToDouble() == value
? value.toInt().toString()
: value.toStringAsFixed(2);
@override
Future<DiscoveryItem> detail({
required String resource,
required int id,
}) async {
final json = await client.getPublicJson('/$resource/$id');
return DiscoveryItem.fromJson(json);
}
}
