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

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

/// 승인자 후보 응답을 파싱하는 DTO.
class ApprovalApproverCandidateDto {
  ApprovalApproverCandidateDto({
    required this.id,
    required this.employeeNo,
    required this.name,
    this.team,
    this.email,
    this.phone,
  });

  final int id;
  final String employeeNo;
  final String name;
  final String? team;
  final String? email;
  final String? phone;

  /// JSON 응답에서 DTO를 생성한다.
  factory ApprovalApproverCandidateDto.fromJson(Map<String, dynamic> json) {
    final group = json['group'] is Map<String, dynamic>
        ? json['group'] as Map<String, dynamic>
        : null;
    return ApprovalApproverCandidateDto(
      id: json['id'] as int? ?? JsonUtils.readInt(json, 'user_id', fallback: 0),
      employeeNo:
          json['employee_id'] as String? ??
          json['employee_no'] as String? ??
          '-',
      name: json['name'] as String? ?? json['employee_name'] as String? ?? '-',
      team: group?['group_name'] as String? ?? json['team'] as String?,
      email: json['email'] as String?,
      phone: json['phone'] as String? ?? json['mobile_no'] as String?,
    );
  }

  /// DTO를 도메인 엔티티로 변환한다.
  ApprovalApproverCandidate toEntity() {
    return ApprovalApproverCandidate(
      id: id,
      employeeNo: employeeNo,
      name: name,
      team: team,
      email: email,
      phone: phone,
    );
  }
}
