import 'package:superport_v2/core/common/utils/json_utils.dart';

import '../../domain/entities/postal_code.dart';

/// 우편번호 검색 API 응답을 표현하는 DTO.
class PostalCodeDto {
  PostalCodeDto({
    required this.zipcode,
    this.sido,
    this.sigungu,
    this.roadName,
    this.buildingMainNo,
    this.buildingSubNo,
  });

  final String zipcode;
  final String? sido;
  final String? sigungu;
  final String? roadName;
  final int? buildingMainNo;
  final int? buildingSubNo;

  factory PostalCodeDto.fromJson(Map<String, dynamic> json) {
    return PostalCodeDto(
      zipcode: json['zipcode'] as String,
      sido: json['sido'] as String?,
      sigungu: json['sigungu'] as String?,
      roadName: json['road_name'] as String?,
      buildingMainNo: _parseInt(json['building_main_no']),
      buildingSubNo: _parseInt(json['building_sub_no']),
    );
  }

  PostalCode toEntity() {
    return PostalCode(
      zipcode: zipcode,
      sido: sido,
      sigungu: sigungu,
      roadName: roadName,
      buildingMainNo: buildingMainNo,
      buildingSubNo: buildingSubNo,
    );
  }

  static List<PostalCode> fromResponse(dynamic data) {
    final items = JsonUtils.extractList(data, keys: const ['items', 'data']);
    if (items.isEmpty) {
      return const [];
    }
    return items
        .map(PostalCodeDto.fromJson)
        .map((dto) => dto.toEntity())
        .toList(growable: false);
  }
}

int? _parseInt(Object? value) {
  if (value == null) {
    return null;
  }
  if (value is int) {
    return value;
  }
  if (value is String) {
    return int.tryParse(value);
  }
  return null;
}
