import 'package:cheminova/services/app_interceptor.dart';
import 'package:dio/dio.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';

class ApiService {
  final Dio _dio;

  ApiService() : _dio = Dio(BaseOptions(baseUrl: 'https://api.cnapp.co.in')) {
    _dio.interceptors.add(AuthInterceptor());
    _dio.interceptors.add(PrettyDioLogger());
  }

  Future<Response> get(String path) async {
    try {
      return await _dio.get(path);
    } on DioException catch (e) {
      return _handleError(e);
    }
  }

  Future<Response> _handleError(DioException e) async {
    if (e.response != null) {
      return e.response!;
    } else {
      return Response(
          requestOptions: RequestOptions(path: ''),
          statusCode: 500,
          data: {'message': 'An unexpected error occurred'});
    }
  }

  Future<Response> post(String path, {dynamic data}) {
    return _dio.post(path, data: data); // Send POST request
  }}