import '../../core/api/api_client.dart';

class PublicApi {
  PublicApi(this._client);

  final ApiClient _client;

  Future<List<Map<String, dynamic>>> discover({
    String? city,
    String? q,
    double? minRating,
    String? sort,
  }) async {
    final params = <String, String>{};
    if (city != null && city.isNotEmpty) params['city'] = city;
    if (q != null && q.isNotEmpty) params['q'] = q;
    if (minRating != null) params['minRating'] = minRating.toString();
    if (sort != null && sort.isNotEmpty) params['sort'] = sort;
    final res = await _client.dio.get<Map<String, dynamic>>(
      '/api/public/discover',
      queryParameters: params.isEmpty ? null : params,
    );
    final list = res.data?['data'] as List<dynamic>? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<List<Map<String, dynamic>>> getReviews(String slug, {int page = 1}) async {
    final res = await _client.dio.get<Map<String, dynamic>>(
      '/api/public/cafes/$slug/reviews',
      queryParameters: {'page': page, 'pageSize': 20},
    );
    final list = res.data?['data'] as List<dynamic>? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<Map<String, dynamic>?> createReview(
    String slug, {
    required String authorName,
    required int rating,
    String? comment,
    String? authorPhone,
  }) async {
    final res = await _client.dio.post<Map<String, dynamic>>(
      '/api/public/cafes/$slug/reviews',
      data: {
        'authorName': authorName,
        'rating': rating,
        if (comment != null && comment.isNotEmpty) 'comment': comment,
        if (authorPhone != null && authorPhone.isNotEmpty) 'authorPhone': authorPhone,
      },
    );
    return res.data?['data'] as Map<String, dynamic>?;
  }

  Future<Map<String, dynamic>?> getCafe(String slug) async {
    final res = await _client.dio.get<Map<String, dynamic>>('/api/public/cafes/$slug');
    return res.data?['data'] as Map<String, dynamic>?;
  }

  Future<Map<String, dynamic>?> getMenu(String slug) async {
    final res = await _client.dio.get<Map<String, dynamic>>('/api/public/cafes/$slug/menu');
    return res.data?['data'] as Map<String, dynamic>?;
  }

  Future<Map<String, dynamic>?> resolveQr(String qrCode) async {
    final res = await _client.dio.get<Map<String, dynamic>>('/api/q/$qrCode');
    return res.data?['data'] as Map<String, dynamic>?;
  }

  Future<Map<String, dynamic>?> placeOrder(
    String slug, {
    required String? tableId,
    required List<Map<String, dynamic>> items,
    String? guestPhone,
    String? guestName,
  }) async {
    final res = await _client.dio.post<Map<String, dynamic>>(
      '/api/public/cafes/$slug/orders',
      data: {
        'orderType': 'DineIn',
        if (tableId != null) 'tableId': tableId,
        if (guestPhone != null) 'guestPhone': guestPhone,
        if (guestName != null) 'guestName': guestName,
        'items': items,
      },
    );
    return res.data?['data'] as Map<String, dynamic>?;
  }

  Future<Map<String, dynamic>?> trackOrder(String orderId) async {
    final res = await _client.dio.get<Map<String, dynamic>>('/api/public/orders/$orderId/track');
    return res.data?['data'] as Map<String, dynamic>?;
  }

  Future<Map<String, dynamic>?> createReservation(
    String slug, {
    required String guestName,
    required String guestPhone,
    required String date,
    required String time,
    required int partySize,
    String? notes,
  }) async {
    final res = await _client.dio.post<Map<String, dynamic>>(
      '/api/public/cafes/$slug/reservations',
      data: {
        'guestName': guestName,
        'guestPhone': guestPhone,
        'date': date,
        'time': time,
        'partySize': partySize,
        if (notes != null) 'notes': notes,
      },
    );
    return res.data?['data'] as Map<String, dynamic>?;
  }
}
