import 'package:dio/dio.dart';
import 'package:food_delivery_client/core/network/header_interceptors.dart';
import 'package:food_delivery_client/food_delivery_client.dart';
import 'error_handler_interceptor.dart';

@module
abstract class DioModule {
  @lazySingleton
  Dio dio(DioClient dioClient) => dioClient.dio;
}

@lazySingleton
class DioClient {
  final BaseOptions _dioBaseOptions = BaseOptions(
    baseUrl: ApiConst.baseUrl,
    connectTimeout: TimeDelayConst.durationMill3500,
    receiveTimeout: TimeDelayConst.durationMill3500,
    followRedirects: false,
    validateStatus: (status) => status != null && status >= 200 && status < 300,
  );

  Dio get dio {
    final Dio dio = Dio(_dioBaseOptions);
    dio.interceptors
      ..add(HeaderInterceptors())
      ..add(ErrorHandlerInterceptors())
      ..add(
        LogInterceptor(
          requestHeader: true,
          requestBody: true,
          responseBody: true,
          error: true,
          request: true,
        ),
      );
    return dio;
  }
}
