import 'package:dio/dio.dart';
import '../../../../core/errors/errors.dart';
import '../../../../core/errors/result.dart';
import '../../domain/entities/server_config.dart';

abstract class SettingsRemoteDataSource {
  Future<Result<ServerConfig>> getServerConfig();
}

class SettingsRemoteDataSourceImpl implements SettingsRemoteDataSource {
  final Dio _dio;

  SettingsRemoteDataSourceImpl(this._dio);

  @override
  Future<Result<ServerConfig>> getServerConfig() async {
    try {
      // Backend ожидает /api/config
      final response = await _dio.get('/api/config');
      if (response.statusCode == 200) {
        final config = ServerConfig.fromJson(response.data as Map<String, dynamic>);
        return Result.success(config);
      }
      return Result.failure(AppError.server(statusCode: 500, message: 'Failed to load config'));
    } on DioException catch (e) {
      return Result.failure(AppError.network(message: e.message));
    } on TypeError catch (e) {
      return Result.failure(AppError.unknown(message: 'Data parsing error: ${e.toString()}'));
    } catch (e) {
      return Result.failure(AppError.unknown(message: e.toString()));
    }
  }
}
