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 position;
  final DateTime otDate;
  final DateTime startTime;
  final DateTime endTime;
  final double otHours;
  final String otType;
  final String compensationType;
  final String reason;
  final String remark;
  final String status;
  final String currentApproverId;
  final List<String> approvalChain;
  final DateTime createTime;
  final DateTime updateTime;
  final double actualOtHours;
  final DateTime? clockInTime;
  final DateTime? clockOutTime;
  final double mealDeductHours;
  final List<ApprovalRecord> approvalRecords;

  const OvertimeModel({
    required this.id,
    required this.applicationNo,
    required this.applicantId,
    required this.applicantName,
    required this.deptId,
    required this.deptName,
    this.position = '',
    required this.otDate,
    required this.startTime,
    required this.endTime,
    required this.otHours,
    required this.otType,
    required this.compensationType,
    required this.reason,
    this.remark = '',
    this.status = 'draft',
    this.currentApproverId = '',
    this.approvalChain = const [],
    required this.createTime,
    required this.updateTime,
    this.actualOtHours = 0.0,
    this.clockInTime,
    this.clockOutTime,
    this.mealDeductHours = 0.5,
    this.approvalRecords = const [],
  });

  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? ?? '',
      position: json['position'] as String? ?? '',
      otDate: DateTime.parse(json['otDate'] as String),
      startTime: DateTime.parse(json['startTime'] as String),
      endTime: DateTime.parse(json['endTime'] as String),
      otHours: (json['otHours'] as num?)?.toDouble() ?? 0.0,
      otType: json['otType'] as String? ?? '工作日加班',
      compensationType: json['compensationType'] as String? ?? '加班费',
      reason: json['reason'] as String? ?? '',
      remark: json['remark'] as String? ?? '',
      status: json['status'] as String? ?? 'draft',
      currentApproverId: json['currentApproverId'] as String? ?? '',
      approvalChain:
          (json['approvalChain'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
      actualOtHours: (json['actualOtHours'] as num?)?.toDouble() ?? 0.0,
      clockInTime: json['clockInTime'] != null
          ? DateTime.parse(json['clockInTime'] as String)
          : null,
      clockOutTime: json['clockOutTime'] != null
          ? DateTime.parse(json['clockOutTime'] as String)
          : null,
      mealDeductHours: (json['mealDeductHours'] as num?)?.toDouble() ?? 0.5,
      approvalRecords:
          (json['approvalRecords'] as List<dynamic>?)
              ?.map((e) => ApprovalRecord.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'applicationNo': applicationNo,
    'applicantId': applicantId,
    'applicantName': applicantName,
    'deptId': deptId,
    'deptName': deptName,
    'position': position,
    'otDate': otDate.toIso8601String(),
    'startTime': startTime.toIso8601String(),
    'endTime': endTime.toIso8601String(),
    'otHours': otHours,
    'otType': otType,
    'compensationType': compensationType,
    'reason': reason,
    'remark': remark,
    'status': status,
    'currentApproverId': currentApproverId,
    'approvalChain': approvalChain,
    'createTime': createTime.toIso8601String(),
    'updateTime': updateTime.toIso8601String(),
    'actualOtHours': actualOtHours,
    'clockInTime': clockInTime?.toIso8601String(),
    'clockOutTime': clockOutTime?.toIso8601String(),
    'mealDeductHours': mealDeductHours,
    'approvalRecords': approvalRecords.map((r) => r.toJson()).toList(),
  };

  OvertimeModel copyWith({
    String? id,
    String? applicationNo,
    String? applicantId,
    String? applicantName,
    String? deptId,
    String? deptName,
    String? position,
    DateTime? otDate,
    DateTime? startTime,
    DateTime? endTime,
    double? otHours,
    String? otType,
    String? compensationType,
    String? reason,
    String? remark,
    String? status,
    String? currentApproverId,
    List<String>? approvalChain,
    DateTime? createTime,
    DateTime? updateTime,
    double? actualOtHours,
    DateTime? clockInTime,
    DateTime? clockOutTime,
    double? mealDeductHours,
    List<ApprovalRecord>? approvalRecords,
  }) {
    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,
      position: position ?? this.position,
      otDate: otDate ?? this.otDate,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      otHours: otHours ?? this.otHours,
      otType: otType ?? this.otType,
      compensationType: compensationType ?? this.compensationType,
      reason: reason ?? this.reason,
      remark: remark ?? this.remark,
      status: status ?? this.status,
      currentApproverId: currentApproverId ?? this.currentApproverId,
      approvalChain: approvalChain ?? this.approvalChain,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      actualOtHours: actualOtHours ?? this.actualOtHours,
      clockInTime: clockInTime ?? this.clockInTime,
      clockOutTime: clockOutTime ?? this.clockOutTime,
      mealDeductHours: mealDeductHours ?? this.mealDeductHours,
      approvalRecords: approvalRecords ?? this.approvalRecords,
    );
  }
}
