import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/network/api_client.dart';
import '../../app.dart';
import 'message_model.dart';

final messageApiProvider = Provider<MessageApi>((ref) => MessageApi(ref.read(apiClientProvider)));

class MessageApi {
  final ApiClient _client;
  MessageApi(this._client);

  Future<List<MessageModel>> fetchList({int page = 1, int pageSize = 20}) async {
    final response = await _client.get<Map<String, dynamic>>(
      '/messages',
      queryParameters: {'page': page, 'pageSize': pageSize},
    );
    final data = response.data!;
    final items = (data['items'] as List<dynamic>)
        .map((e) => MessageModel.fromJson(e as Map<String, dynamic>))
        .toList();
    return items;
  }
}
