import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
import '../../../../core/error/exceptions.dart';
import '../../../../core/network/api_client.dart';
import '../../../../core/utils/logger.dart';
import '../models/compliance_snapshot_model.dart';

abstract class ComplianceRemoteDataSource {
  /// GET /api/compliance/dashboard — snapshot pour l'organisation active.
  Future<ComplianceSnapshotModel> getSnapshotCurrent();
}

@Injectable(as: ComplianceRemoteDataSource)
class ComplianceRemoteDataSourceImpl implements ComplianceRemoteDataSource {
  final ApiClient apiClient;

  ComplianceRemoteDataSourceImpl(this.apiClient);

  @override
  Future<ComplianceSnapshotModel> getSnapshotCurrent() async {
    try {
      final response = await apiClient.get('/api/compliance/dashboard');
      if (response.statusCode == 200 && response.data is Map<String, dynamic>) {
        return ComplianceSnapshotModel.fromJson(
          response.data as Map<String, dynamic>,
        );
      }
      throw ServerException('Compliance snapshot HTTP ${response.statusCode}');
    } on DioException catch (e) {
      AppLogger.error('ComplianceRemoteDataSource: getSnapshotCurrent', error: e);
      rethrow;
    } catch (e, st) {
      AppLogger.error('ComplianceRemoteDataSource: getSnapshotCurrent',
          error: e, stackTrace: st);
      rethrow;
    }
  }
}
