import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/network/api_client.dart';
import '../../app.dart';
import '../../shared/models/pagination_model.dart';
import 'announcement_model.dart';

final announcementApiProvider = Provider<AnnouncementApi>(
  (ref) => AnnouncementApi(ref.read(apiClientProvider)),
);

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

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

  Future<AnnouncementModel> fetchDetail(String id) async {
    final response = await _client.get<Map<String, dynamic>>(
      '/announcement/detail/$id',
    );
    return AnnouncementModel.fromJson(response.data!);
  }
}
