import '../../../../../core/common/utils/json_utils.dart';
import '../../domain/entities/lookup_item.dart';

/// 룩업 API 응답을 표현하는 DTO.
class LookupItemDto {
  LookupItemDto({
    required this.id,
    required this.name,
    this.code,
    this.isDefault = false,
    this.isActive = true,
    this.note,
  });

  final int id;
  final String? code;
  final String name;
  final bool isDefault;
  final bool isActive;
  final String? note;

  factory LookupItemDto.fromJson(Map<String, dynamic> json) {
    return LookupItemDto(
      id: json['id'] as int? ?? 0,
      code: json['code'] as String? ?? json['status_code'] as String?,
      name:
          json['name'] as String? ??
          json['type_name'] as String? ??
          json['status_name'] as String? ??
          json['action_name'] as String? ??
          '',
      isDefault: (json['is_default'] as bool?) ?? false,
      isActive: (json['is_active'] as bool?) ?? true,
      note: json['note'] as String?,
    );
  }

  LookupItem toEntity() => LookupItem(
    id: id,
    code: code,
    name: name,
    isDefault: isDefault,
    isActive: isActive,
    note: note,
  );

  static List<LookupItem> parseList(Map<String, dynamic>? json) {
    final rawItems = JsonUtils.extractList(json, keys: const ['items']);
    return rawItems
        .map(LookupItemDto.fromJson)
        .map((dto) => dto.toEntity())
        .toList(growable: false);
  }
}
