import 'package:anyway/core/dio_client.dart';
import 'package:anyway/data/repositories/local_onboarding_repository.dart';
import 'package:anyway/domain/repositories/onboarding_repository.dart';
import 'package:anyway/domain/repositories/trip_repository.dart';
import 'package:anyway/data/repositories/backend_trip_repository.dart';
import 'package:anyway/data/datasources/trip_remote_datasource.dart';
import 'package:anyway/data/repositories/preferences_repository_impl.dart';
import 'package:anyway/domain/repositories/preferences_repository.dart';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

/// Provide a configured Dio instance (used by data layer)
final dioProvider = Provider<Dio>((ref) {
  // baseUrl can be configured via environment; use a sensible default
  return Dio(BaseOptions(
    baseUrl: 'https://anyway.anydev.info',
    connectTimeout: const Duration(seconds: 5),
    receiveTimeout: const Duration(seconds: 120),
    headers: {
      HttpHeaders.acceptHeader: 'application/json',
      HttpHeaders.contentTypeHeader: 'application/json',
    },
  ));
});

/// Provide a simple wrapper client if needed elsewhere
final dioClientProvider = Provider<DioClient>((ref) {
  final dio = ref.read(dioProvider);
  return DioClient(baseUrl: dio.options.baseUrl);
});

/// Onboarding repository backed by SharedPreferences
final onboardingRepositoryProvider = Provider<OnboardingRepository>((ref) {
  return LocalOnboardingRepository();
});

/// Preferences repository (local persistence)
final preferencesRepositoryProvider = Provider<PreferencesRepository>((ref) {
  return PreferencesRepositoryImpl();
});

/// Trip repository backed by the backend
final tripRepositoryProvider = Provider<TripRepository>((ref) {
  final dio = ref.read(dioProvider);
  final remote = TripRemoteDataSourceImpl(dio: dio);
  return BackendTripRepository(remote: remote);
});
