import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

import '../../../../core/network/api_client.dart';
import '../../../../core/network/api_routes.dart';
import '../../domain/entities/dashboard_summary.dart';
import '../../domain/repositories/dashboard_repository.dart';
import '../dtos/dashboard_summary_dto.dart';

/// 대시보드 요약 데이터를 불러오는 원격 저장소.
class DashboardRepositoryRemote implements DashboardRepository {
  DashboardRepositoryRemote({required ApiClient apiClient}) : _api = apiClient;

  final ApiClient _api;

  static const _summaryPath = '${ApiRoutes.apiV1}/dashboard/summary';

  @override
  Future<DashboardSummary> fetchSummary() async {
    final response = await _api.get<Map<String, dynamic>>(
      _summaryPath,
      options: Options(responseType: ResponseType.json),
    );
    if (kDebugMode) {
      try {
        debugPrint(
          '[DashboardRepositoryRemote] dashboard/summary 응답: ${jsonEncode(response.data)}',
        );
      } catch (error) {
        debugPrint(
          '[DashboardRepositoryRemote] dashboard/summary 응답 직렬화 실패: $error',
        );
      }
    }
    return DashboardSummaryDto.fromJson(_api.unwrapAsMap(response)).toEntity();
  }
}
