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

final overtimeApiProvider = Provider<OvertimeApi>(
  (ref) => OvertimeApi(ref.read(apiClientProvider)),
);

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

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

  Future<void> submit(OvertimeModel model) async {
    await _client.post('/overtime/apply', data: model.toJson());
  }

  Future<void> saveDraft(OvertimeModel model) async {
    await _client.put('/overtime/draft', data: model.toJson());
  }
}
