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

class ExpenseApplicationModel {
  final String id;
  final String applicationNo;
  final String applicantId;
  final String applicantName;
  final String deptId;
  final String deptName;
  final String expenseType;
  final double estimatedAmount;
  final String purpose;
  final String remark;
  final String status;
  final String currentApproverId;
  final List<String> approvalChain;
  final DateTime createTime;
  final DateTime updateTime;
  final DateTime? estimatedStartDate;
  final DateTime? estimatedEndDate;
  final String urgency;
  final String projectId;
  final String projectName;
  final String budgetSubjectId;
  final List<ExpenseAppDetailModel> details;
  final List<ApprovalRecord> approvalRecords;

  const ExpenseApplicationModel({
    required this.id,
    required this.applicationNo,
    this.applicantId = '',
    this.applicantName = '',
    this.deptId = '',
    this.deptName = '',
    this.expenseType = '',
    this.estimatedAmount = 0.0,
    this.purpose = '',
    this.remark = '',
    this.status = 'draft',
    this.currentApproverId = '',
    this.approvalChain = const [],
    required this.createTime,
    required this.updateTime,
    this.estimatedStartDate,
    this.estimatedEndDate,
    this.urgency = 'normal',
    this.projectId = '',
    this.projectName = '',
    this.budgetSubjectId = '',
    this.details = const [],
    this.approvalRecords = const [],
  });

  factory ExpenseApplicationModel.fromJson(Map<String, dynamic> json) {
    return ExpenseApplicationModel(
      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? ?? '',
      expenseType: json['expenseType'] as String? ?? '',
      estimatedAmount: (json['estimatedAmount'] as num?)?.toDouble() ?? 0.0,
      purpose: json['purpose'] 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),
      estimatedStartDate: json['estimatedStartDate'] != null
          ? DateTime.parse(json['estimatedStartDate'] as String)
          : null,
      estimatedEndDate: json['estimatedEndDate'] != null
          ? DateTime.parse(json['estimatedEndDate'] as String)
          : null,
      urgency: json['urgency'] as String? ?? 'normal',
      projectId: json['projectId'] as String? ?? '',
      projectName: json['projectName'] as String? ?? '',
      budgetSubjectId: json['budgetSubjectId'] as String? ?? '',
      details:
          (json['details'] as List<dynamic>?)
              ?.map(
                (e) =>
                    ExpenseAppDetailModel.fromJson(e as Map<String, dynamic>),
              )
              .toList() ??
          [],
      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,
    'expenseType': expenseType,
    'estimatedAmount': estimatedAmount,
    'purpose': purpose,
    'remark': remark,
    'status': status,
    'currentApproverId': currentApproverId,
    'approvalChain': approvalChain,
    'createTime': createTime.toIso8601String(),
    'updateTime': updateTime.toIso8601String(),
    'estimatedStartDate': estimatedStartDate?.toIso8601String(),
    'estimatedEndDate': estimatedEndDate?.toIso8601String(),
    'urgency': urgency,
    'projectId': projectId,
    'projectName': projectName,
    'budgetSubjectId': budgetSubjectId,
    'details': details.map((d) => d.toJson()).toList(),
    'approvalRecords': approvalRecords.map((r) => r.toJson()).toList(),
  };

  ExpenseApplicationModel copyWith({
    String? id,
    String? applicationNo,
    String? applicantId,
    String? applicantName,
    String? deptId,
    String? deptName,
    String? expenseType,
    double? estimatedAmount,
    String? purpose,
    String? remark,
    String? status,
    String? currentApproverId,
    List<String>? approvalChain,
    DateTime? createTime,
    DateTime? updateTime,
    DateTime? estimatedStartDate,
    DateTime? estimatedEndDate,
    String? urgency,
    String? projectId,
    String? projectName,
    String? budgetSubjectId,
    List<ExpenseAppDetailModel>? details,
    List<ApprovalRecord>? approvalRecords,
  }) {
    return ExpenseApplicationModel(
      id: id ?? this.id,
      applicationNo: applicationNo ?? this.applicationNo,
      applicantId: applicantId ?? this.applicantId,
      applicantName: applicantName ?? this.applicantName,
      deptId: deptId ?? this.deptId,
      deptName: deptName ?? this.deptName,
      expenseType: expenseType ?? this.expenseType,
      estimatedAmount: estimatedAmount ?? this.estimatedAmount,
      purpose: purpose ?? this.purpose,
      remark: remark ?? this.remark,
      status: status ?? this.status,
      currentApproverId: currentApproverId ?? this.currentApproverId,
      approvalChain: approvalChain ?? this.approvalChain,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      estimatedStartDate: estimatedStartDate ?? this.estimatedStartDate,
      estimatedEndDate: estimatedEndDate ?? this.estimatedEndDate,
      urgency: urgency ?? this.urgency,
      projectId: projectId ?? this.projectId,
      projectName: projectName ?? this.projectName,
      budgetSubjectId: budgetSubjectId ?? this.budgetSubjectId,
      details: details ?? this.details,
      approvalRecords: approvalRecords ?? this.approvalRecords,
    );
  }
}

class ExpenseAppDetailModel {
  final String id;
  final String applicationId;
  final String itemName;
  final double estimatedAmount;
  final String remark;
  final int sortOrder;

  const ExpenseAppDetailModel({
    required this.id,
    this.applicationId = '',
    this.itemName = '',
    this.estimatedAmount = 0.0,
    this.remark = '',
    this.sortOrder = 1,
  });

  factory ExpenseAppDetailModel.fromJson(Map<String, dynamic> json) {
    return ExpenseAppDetailModel(
      id: json['id'] as String,
      applicationId: json['applicationId'] as String? ?? '',
      itemName: json['itemName'] as String? ?? '',
      estimatedAmount: (json['estimatedAmount'] as num?)?.toDouble() ?? 0.0,
      remark: json['remark'] as String? ?? '',
      sortOrder: json['sortOrder'] as int? ?? 1,
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'applicationId': applicationId,
    'itemName': itemName,
    'estimatedAmount': estimatedAmount,
    'remark': remark,
    'sortOrder': sortOrder,
  };
}
