package ru.soune.nocopy.dto.payout;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.soune.nocopy.entity.payout.PayoutRequest;
import ru.soune.nocopy.entity.payout.PayoutRequestStatus;
import ru.soune.nocopy.entity.payout.PayoutType;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor

public class PayoutResponse {
    private Long id;
    private BigDecimal amount;
    private String formattedAmount;
    private PayoutRequestStatus status;
    private PayoutType payoutType;
    private String payoutMethodDisplay;
    private LocalDateTime createdAt;
    private String formattedCreatedAt;
    private LocalDateTime processedAt;
    private LocalDateTime completedAt;
    private String transactionId;
    private String rejectionReason;
    private boolean canCancel;

    public static PayoutResponse fromEntity(PayoutRequest request) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

        return PayoutResponse.builder()
                .id(request.getId())
                .amount(request.getAmount())
                .formattedAmount(String.format("%.2f ₽", request.getAmount()))
                .status(request.getStatus())
                .payoutType(request.getPayoutType())
                .payoutMethodDisplay(request.getPayoutMethod() != null ?
                        request.getPayoutMethod().getDisplayName() : null)
                .createdAt(request.getCreatedAt())
                .formattedCreatedAt(request.getCreatedAt() != null ?
                        request.getCreatedAt().format(formatter) : null)
                .processedAt(request.getProcessAt())
                .completedAt(request.getCompletedAt())
                .rejectionReason(request.getRejectionReason())
                .canCancel(request.getStatus() == PayoutRequestStatus.PENDING)
                .build();
    }
}
