import 'package:dio/dio.dart';

import '../../../../core/network/api_client.dart';
import '../../../../core/network/api_routes.dart';
import '../../domain/entities/auth_session.dart';
import '../../domain/entities/login_request.dart';
import '../../domain/repositories/auth_repository.dart';
import '../dtos/auth_session_dto.dart';

/// 인증 관련 엔드포인트를 호출하는 원격 저장소 구현체.
class AuthRepositoryRemote implements AuthRepository {
  AuthRepositoryRemote({required ApiClient apiClient}) : _api = apiClient;

  final ApiClient _api;

  static const _basePath = '${ApiRoutes.apiV1}/auth';

  @override
  Future<AuthSession> login(LoginRequest request) async {
    final response = await _api.post<Map<String, dynamic>>(
      '$_basePath/login',
      data: {
        'identifier': request.identifier,
        'password': request.password,
        'remember_me': request.rememberMe,
      },
      options: Options(responseType: ResponseType.json),
    );
    return AuthSessionDto.fromJson(_api.unwrapAsMap(response)).toEntity();
  }

  @override
  Future<AuthSession> refresh(String refreshToken) async {
    final response = await _api.post<Map<String, dynamic>>(
      '$_basePath/refresh',
      data: {'refresh_token': refreshToken},
      options: Options(responseType: ResponseType.json),
    );
    return AuthSessionDto.fromJson(_api.unwrapAsMap(response)).toEntity();
  }
}
