package adminfinance

import (
	"time"
)

type financeDetailRow struct {
	OrderID                     uint64
	OrderNo                     string
	OrderStatus                 string
	SettlementStatus            string
	RefundStatus                string
	RenterID                    uint64
	RenterPhone                 string
	RenterNickname              string
	OwnerID                     uint64
	OwnerPhone                  string
	OwnerNickname               string
	OrderRentAmountCent         int64
	OrderDepositAmountCent      int64
	CheckoutRentAmountCent      int64
	CheckoutRenterRefundCent    int64
	CheckoutOwnerIncomeCent     int64
	CheckoutPlatformFeeCent     int64
	OwnerWalletIncomeAmountCent int64
	PaidAmountCent              int64
	RefundedAmountCent          int64
	RefundingAmountCent         int64
	FailedRefundAmountCent      int64
	ChannelNetAmountCent        int64
	PlatformNetAmountCent       int64
	SettlementDiffAmountCent    int64
	FinanceStatus               string
	CreatedAt                   time.Time
	SettledAt                   *time.Time
}

func (r financeDetailRow) toDTO() FinanceDetailDTO {
	diff := r.SettlementDiffAmountCent
	status := r.FinanceStatus
	if status == "" {
		status = "normal"
	}
	if absCent(diff) < 5 && status == "settlement_diff" {
		status = "normal"
	}
	return FinanceDetailDTO{
		OrderID:                     r.OrderID,
		OrderNo:                     r.OrderNo,
		OrderStatus:                 r.OrderStatus,
		SettlementStatus:            r.SettlementStatus,
		RefundStatus:                r.RefundStatus,
		RenterID:                    r.RenterID,
		RenterPhone:                 r.RenterPhone,
		RenterNickname:              r.RenterNickname,
		OwnerID:                     r.OwnerID,
		OwnerPhone:                  r.OwnerPhone,
		OwnerNickname:               r.OwnerNickname,
		OrderRentAmountCent:         r.OrderRentAmountCent,
		OrderDepositAmountCent:      r.OrderDepositAmountCent,
		CheckoutRentAmountCent:      r.CheckoutRentAmountCent,
		CheckoutRenterRefundCent:    r.CheckoutRenterRefundCent,
		CheckoutOwnerIncomeCent:     r.CheckoutOwnerIncomeCent,
		CheckoutPlatformFeeCent:     r.CheckoutPlatformFeeCent,
		OwnerWalletIncomeAmountCent: r.OwnerWalletIncomeAmountCent,
		PaidAmountCent:              r.PaidAmountCent,
		RefundedAmountCent:          r.RefundedAmountCent,
		RefundingAmountCent:         r.RefundingAmountCent,
		FailedRefundAmountCent:      r.FailedRefundAmountCent,
		ChannelNetAmountCent:        r.ChannelNetAmountCent,
		PlatformNetAmountCent:       r.PlatformNetAmountCent,
		SettlementDiffAmountCent:    diff,
		FinanceStatus:               status,
		CreatedAt:                   r.CreatedAt,
		SettledAt:                   r.SettledAt,
	}
}
