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

final vehicleApiProvider = Provider<VehicleApi>(
  (ref) => VehicleApi(ref.read(apiClientProvider)),
);

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

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

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

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