import 'package:dio/dio.dart';

import '../../models/admin_request.dart';

/// User-side requests API — `/api/requests`. Server scopes results to
/// the caller; admins see only their own here, not all users'. The
/// admin-cross-user view lives in `/api/admin/requests` (AdminRequestsApi).
///
/// Wire shape is identical to the admin endpoint, so the same
/// AdminRequest model is reused.
class RequestsApi {
  RequestsApi(this._dio);
  final Dio _dio;

  /// GET /api/requests — caller's own requests, all statuses.
  Future<List<AdminRequest>> listMine() async {
    final r = await _dio.get<List<dynamic>>('/api/requests');
    final raw = r.data ?? const [];
    return raw
        .map((e) => AdminRequest.fromJson((e as Map).cast<String, dynamic>()))
        .toList(growable: false);
  }

  /// DELETE /api/requests/{id} — cancel a pending request. Server
  /// returns the cancelled row body (not 204) so the caller can patch
  /// local state without a refetch.
  Future<AdminRequest> cancel(String id) async {
    final r = await _dio.delete<Map<String, dynamic>>('/api/requests/$id');
    return AdminRequest.fromJson(r.data ?? const {});
  }
}
