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

final outingLogApiProvider = Provider<OutingLogApi>(
  (ref) => OutingLogApi(ref.read(apiClientProvider)),
);

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

  Future<PaginatedData<OutingLogModel>> fetchList({
    int page = 1,
    int size = 20,
  }) async {
    final response = await _client.get<Map<String, dynamic>>(
      '/outing-log/list',
      queryParameters: {'page': page, 'size': size},
    );
    final data = response.data!;
    final list = (data['list'] as List<dynamic>)
        .map((e) => OutingLogModel.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<OutingLogModel> fetchDetail(String id) async {
    final response = await _client.get<Map<String, dynamic>>(
      '/outing-log/detail/$id',
    );
    return OutingLogModel.fromJson(response.data!);
  }

  Future<void> create(OutingLogModel model) async {
    await _client.post('/outing-log/create', data: model.toJson());
  }
}
