import 'package:dio/dio.dart';

import '../../models/invite.dart';

class AdminInvitesApi {
  AdminInvitesApi(this._dio);
  final Dio _dio;

  /// GET /api/admin/invites → `{"invites": [...]}`. Envelope unwrapped.
  Future<List<Invite>> list() async {
    final r = await _dio.get<Map<String, dynamic>>('/api/admin/invites');
    final raw = (r.data?['invites'] as List?) ?? const [];
    return raw
        .map((e) => Invite.fromJson((e as Map).cast<String, dynamic>()))
        .toList(growable: false);
  }

  /// POST /api/admin/invites — body is optional `{"note": "..."}`.
  /// Server hardcodes the 24h TTL; admins cannot configure expiry.
  /// Returns the bare invite (not enveloped).
  Future<Invite> create({String? note}) async {
    final r = await _dio.post<Map<String, dynamic>>(
      '/api/admin/invites',
      data: {if (note != null && note.isNotEmpty) 'note': note},
    );
    return Invite.fromJson(r.data ?? const {});
  }

  Future<void> revoke(String token) async {
    await _dio.delete<void>('/api/admin/invites/$token');
  }
}
