class SubmitRefundProcedureItem {
  final int lineItemNo;
  final String procedureId;
  final String procedureName;
  final double patientPaidAmount;
  final String refundReason;

  const SubmitRefundProcedureItem({
    required this.lineItemNo,
    required this.procedureId,
    required this.procedureName,
    required this.patientPaidAmount,
    required this.refundReason,
  });

  Map<String, dynamic> toJson() => {
        "LineItemNo": lineItemNo,
        "ProcedureID": procedureId,
        "ProcedureName": procedureName,
        "PatientPaidAmount": patientPaidAmount,
        "RefundReason": refundReason,
      };

  @override
  String toString() {
    return 'SubmitRefundProcedureItem{lineItemNo: $lineItemNo, procedureId: $procedureId, procedureName: $procedureName, patientPaidAmount: $patientPaidAmount, refundReason: $refundReason}';
  }
}

/// Represents a single advance item in the wallet withdrawal refund payload.
class SubmitRefundAdvanceItem {
  final String advanceNo;
  final double paidAmount;
  final double balanceAmount;
  final String advanceDate;

  const SubmitRefundAdvanceItem({
    required this.advanceNo,
    required this.paidAmount,
    required this.balanceAmount,
    required this.advanceDate,
  });

  Map<String, dynamic> toJson() => {
        "AdvanceNo": advanceNo,
        "PaidAmount": paidAmount,
        "BalanceAmount": balanceAmount,
        "AdvanceDate": advanceDate,
      };

  @override
  String toString() {
    return 'SubmitRefundAdvanceItem{advanceNo: $advanceNo, paidAmount: $paidAmount, balanceAmount: $balanceAmount, advanceDate: $advanceDate}';
  }
}

class SubmitRefundRequestModel {
  final int projectId;
  final String refundType;
  final String refundProject;
  final int refundProjectId;
  final String refundPatientId;
  final String patientMobileNumber;
  final String refundPatientName;
  final String refundNationalId;
  final String refundInvoiceNo;
  final String refundAppointmentNo;
  final int refundDoctorId;
  final String refundDoctorName;
  final int refundClinicId;
  final String refundClinicName;
  final String refundDestination;
  final String refundIban;
  final String refundRemarks;
  final double refundTotalAmount;
  final bool isRefundFastTrack;
  final List<SubmitRefundProcedureItem> refundProceduresList;

  /// Populated only for wallet withdrawal refunds (refundType == '2').
  final List<SubmitRefundAdvanceItem> refundAdvancesList;

  const SubmitRefundRequestModel({
    required this.projectId,
    required this.refundType,
    required this.refundProject,
    required this.refundProjectId,
    required this.refundPatientId,
    required this.patientMobileNumber,
    required this.refundPatientName,
    required this.refundNationalId,
    required this.refundInvoiceNo,
    required this.refundAppointmentNo,
    required this.refundDoctorId,
    required this.refundDoctorName,
    required this.refundClinicId,
    required this.refundClinicName,
    required this.refundDestination,
    required this.refundIban,
    required this.refundRemarks,
    required this.refundTotalAmount,
    required this.isRefundFastTrack,
    required this.refundProceduresList,
    this.refundAdvancesList = const [],
  });

  /// refundType '1' = invoice refund  →  sends Refund_ProceduresList
  /// refundType '2' = wallet withdrawal →  sends Refund_AdvancesList
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> json = {
      "ProjectID": projectId,
      "Refund_RefundType": refundType,
      "Refund_Project": refundProject,
      "Refund_ProjectId": refundProjectId,
      "Refund_PatientId": refundPatientId,
      "PatientMobileNumber": patientMobileNumber,
      "Refund_PatientName": refundPatientName,
      "Refund_NationalId": refundNationalId,
      "Refund_DoctorId": refundDoctorId,
      "Refund_DoctorName": refundDoctorName,
      "Refund_RefundDestination": refundDestination,
      "Refund_IBAN": refundIban,
      "Refund_Remarks": refundRemarks,
      "Refund_TotalRefundAmount": refundTotalAmount,
      "Refund_IsFastTrack": isRefundFastTrack,
    };

    if (refundType == '2') {
      // Wallet withdrawal — include advances list, omit invoice-specific fields
      json["Refund_AdvancesList"] = refundAdvancesList.map((a) => a.toJson()).toList();
    } else {
      // Invoice refund — include invoice-specific fields and procedures list
      json["Refund_InvoiceNo"] = refundInvoiceNo;
      json["Refund_AppointmentNo"] = refundAppointmentNo;
      json["Refund_ClinicId"] = refundClinicId;
      json["Refund_ClinicName"] = refundClinicName;
      json["Refund_ProceduresList"] = refundProceduresList.map((p) => p.toJson()).toList();
    }

    return json;
  }

  @override
  String toString() {
    return 'SubmitRefundRequestModel{projectId: $projectId, refundType: $refundType, refundAdvancesList: $refundAdvancesList, refundProceduresList: $refundProceduresList}';
  }
}
