import 'package:dio/dio.dart';

import 'package:superport_v2/core/network/api_client.dart';
import 'package:superport_v2/core/network/api_routes.dart';

import '../../domain/entities/postal_code.dart';
import '../../domain/repositories/postal_search_repository.dart';
import '../dtos/postal_code_dto.dart';

/// 우편번호 검색 API를 호출하는 원격 저장소 구현체.
class PostalSearchRepositoryRemote implements PostalSearchRepository {
  PostalSearchRepositoryRemote({required ApiClient apiClient})
    : _api = apiClient;

  final ApiClient _api;

  static const _path = '${ApiRoutes.apiV1}/zipcodes';

  @override
  Future<List<PostalCode>> search({
    required String keyword,
    int limit = 20,
    int page = 1,
  }) async {
    final trimmed = keyword.trim();
    if (trimmed.isEmpty) {
      return const [];
    }

    final response = await _api.get<dynamic>(
      _path,
      query: {'q': trimmed, 'page': page, 'page_size': limit},
      options: Options(responseType: ResponseType.json),
    );

    return PostalCodeDto.fromResponse(response.data);
  }
}
