import 'package:dio/dio.dart';

import '../config.dart';
import '../storage/token_storage.dart';

/// کلاینت HTTP با تزریق خودکار توکن Bearer.
class ApiClient {
  final Dio dio;

  ApiClient(TokenStorage storage)
      : dio = Dio(BaseOptions(
          baseUrl: AppConfig.apiUrl,
          connectTimeout: const Duration(seconds: 10),
          receiveTimeout: const Duration(seconds: 10),
          headers: {'Accept': 'application/json'},
        )) {
    dio.interceptors.add(InterceptorsWrapper(
      onRequest: (options, handler) async {
        final token = await storage.read();
        if (token != null && token.isNotEmpty) {
          options.headers['Authorization'] = 'Bearer $token';
        }
        handler.next(options);
      },
    ));
  }
}
