import '../../shared/models/approval_status.dart';

class OvertimeModel {
  final String id;
  final String applicationNo;
  final String applicantId;
  final String applicantName;
  final String deptId;
  final String deptName;
  final String otType;
  final String compensationType;
  final double? compLeaveRatio;
  final DateTime startTime;
  final DateTime endTime;
  final double netOtHours;
  final String reason;
  final String status;
  final String approvalInstanceId;
  final DateTime createTime;
  final DateTime updateTime;
  // 审批相关
  final List<ApprovalRecord> approvalRecords;
  final List<String> approvalChain;
  final String currentApproverId;

  const OvertimeModel({
    required this.id,
    required this.applicationNo,
    required this.applicantId,
    required this.applicantName,
    required this.deptId,
    required this.deptName,
    required this.otType,
    required this.compensationType,
    this.compLeaveRatio,
    required this.startTime,
    required this.endTime,
    required this.netOtHours,
    required this.reason,
    this.status = 'draft',
    this.approvalInstanceId = '',
    required this.createTime,
    required this.updateTime,
    this.approvalRecords = const [],
    this.approvalChain = const [],
    this.currentApproverId = '',
  });

  double get otHours => netOtHours;
  DateTime get otDate => startTime;

  factory OvertimeModel.fromJson(Map<String, dynamic> json) {
    return OvertimeModel(
      id: json['id'] as String,
      applicationNo: json['applicationNo'] as String? ?? '',
      applicantId: json['applicantId'] as String? ?? '',
      applicantName: json['applicantName'] as String? ?? '',
      deptId: json['deptId'] as String? ?? '',
      deptName: json['deptName'] as String? ?? '',
      otType: json['otType'] as String? ?? 'workday',
      compensationType: json['compensationType'] as String? ?? 'overtime_pay',
      compLeaveRatio: (json['compLeaveRatio'] as num?)?.toDouble(),
      startTime: DateTime.parse(json['startTime'] as String),
      endTime: DateTime.parse(json['endTime'] as String),
      netOtHours: (json['netOtHours'] as num?)?.toDouble() ?? 0.0,
      reason: json['reason'] as String? ?? '',
      status: json['status'] as String? ?? 'draft',
      approvalInstanceId: json['approvalInstanceId'] as String? ?? '',
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
      approvalRecords:
          (json['approvalRecords'] as List<dynamic>?)
              ?.map((e) => ApprovalRecord.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      approvalChain:
          (json['approvalChain'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      currentApproverId: json['currentApproverId'] as String? ?? '',
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'applicationNo': applicationNo,
    'applicantId': applicantId,
    'applicantName': applicantName,
    'deptId': deptId,
    'deptName': deptName,
    'otType': otType,
    'compensationType': compensationType,
    'compLeaveRatio': compLeaveRatio,
    'startTime': startTime.toIso8601String(),
    'endTime': endTime.toIso8601String(),
    'netOtHours': netOtHours,
    'reason': reason,
    'status': status,
    'approvalInstanceId': approvalInstanceId,
    'createTime': createTime.toIso8601String(),
    'updateTime': updateTime.toIso8601String(),
    'approvalRecords': approvalRecords.map((r) => r.toJson()).toList(),
    'approvalChain': approvalChain,
    'currentApproverId': currentApproverId,
  };

  OvertimeModel copyWith({
    String? id,
    String? applicationNo,
    String? applicantId,
    String? applicantName,
    String? deptId,
    String? deptName,
    String? otType,
    String? compensationType,
    double? compLeaveRatio,
    DateTime? startTime,
    DateTime? endTime,
    double? netOtHours,
    String? reason,
    String? status,
    String? approvalInstanceId,
    DateTime? createTime,
    DateTime? updateTime,
    List<ApprovalRecord>? approvalRecords,
    List<String>? approvalChain,
    String? currentApproverId,
  }) {
    return OvertimeModel(
      id: id ?? this.id,
      applicationNo: applicationNo ?? this.applicationNo,
      applicantId: applicantId ?? this.applicantId,
      applicantName: applicantName ?? this.applicantName,
      deptId: deptId ?? this.deptId,
      deptName: deptName ?? this.deptName,
      otType: otType ?? this.otType,
      compensationType: compensationType ?? this.compensationType,
      compLeaveRatio: compLeaveRatio ?? this.compLeaveRatio,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      netOtHours: netOtHours ?? this.netOtHours,
      reason: reason ?? this.reason,
      status: status ?? this.status,
      approvalInstanceId: approvalInstanceId ?? this.approvalInstanceId,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      approvalRecords: approvalRecords ?? this.approvalRecords,
      approvalChain: approvalChain ?? this.approvalChain,
      currentApproverId: currentApproverId ?? this.currentApproverId,
    );
  }
}
