import 'package:dio/dio.dart';

import '../demo/demo_marianumconnect_interceptor.dart';
import 'auth/auth_interceptor.dart';

/// Singleton dio instance for the Marianum-Connect mobile API. Wired with the
/// bearer auth interceptor at startup; the base URL is resolved per request
/// through [MarianumConnectEndpoint] so settings changes take effect without
/// recreating the client.
class MarianumConnectApi {
  static const Duration _connectTimeout = Duration(seconds: 10);
  static const Duration _receiveTimeout = Duration(seconds: 20);
  static const Duration _plainReceiveTimeout = Duration(seconds: 15);

  static final Dio _instance = _build();

  static Dio dio() => _instance;

  /// A fresh dio with the standard JSON options but no interceptors — used by
  /// the auth queries (login/verify) that must bypass the bearer/demo
  /// interceptors to avoid a re-auth loop.
  static Dio plainDio() => Dio(
    BaseOptions(
      connectTimeout: _connectTimeout,
      sendTimeout: _connectTimeout,
      receiveTimeout: _plainReceiveTimeout,
      responseType: ResponseType.json,
      contentType: 'application/json',
    ),
  );

  static Dio _build() {
    final dio = Dio(
      BaseOptions(
        connectTimeout: _connectTimeout,
        sendTimeout: _connectTimeout,
        receiveTimeout: _receiveTimeout,
        responseType: ResponseType.json,
        contentType: 'application/json',
      ),
    );
    dio.interceptors.add(DemoMarianumConnectInterceptor());
    dio.interceptors.add(MarianumConnectAuthInterceptor());
    return dio;
  }
}
