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

/// 우편번호 검색 결과를 표현하는 모델.
class PostalSearchResult {
  PostalSearchResult({
    required this.zipcode,
    this.sido,
    this.sigungu,
    this.roadName,
    this.buildingNumber,
  });

  final String zipcode;
  final String? sido;
  final String? sigungu;
  final String? roadName;
  final String? buildingNumber;

  factory PostalSearchResult.fromPostalCode(PostalCode postalCode) {
    final buildingValue = postalCode.buildingNumber;
    return PostalSearchResult(
      zipcode: postalCode.zipcode,
      sido: postalCode.sido,
      sigungu: postalCode.sigungu,
      roadName: postalCode.roadName,
      buildingNumber: buildingValue.isEmpty ? null : buildingValue,
    );
  }

  /// 주소 구성요소를 공백으로 이어 붙인 표시 문자열.
  String get fullAddress {
    final segments = <String>[];
    final sidoValue = sido;
    if (sidoValue != null && sidoValue.isNotEmpty) {
      segments.add(sidoValue);
    }
    final sigunguValue = sigungu;
    if (sigunguValue != null && sigunguValue.isNotEmpty) {
      segments.add(sigunguValue);
    }
    final roadNameValue = roadName;
    if (roadNameValue != null && roadNameValue.isNotEmpty) {
      segments.add(roadNameValue);
    }
    final buildingNumberValue = buildingNumber;
    if (buildingNumberValue != null && buildingNumberValue.isNotEmpty) {
      segments.add(buildingNumberValue);
    }
    return segments.join(' ');
  }
}
