package order

import (
	"time"

	"hfb_sys/backend/internal/auditlog"

	"gorm.io/datatypes"
)

type OrderDTO struct {
	ID                        uint64            `json:"id"`
	OrderNo                   string            `json:"order_no"`
	ListingID                 uint64            `json:"listing_id"`
	ListingNo                 string            `json:"listing_no"`
	AccountID                 uint64            `json:"account_id"`
	OwnerID                   uint64            `json:"owner_id"`
	RenterID                  uint64            `json:"renter_id"`
	OwnerPhone                string            `json:"owner_phone,omitempty"`
	RenterPhone               string            `json:"renter_phone,omitempty"`
	Title                     string            `json:"title"`
	ServerRegion              string            `json:"server_region"`
	LoginPlatform             string            `json:"login_platform"`
	RentedAt                  *time.Time        `json:"rented_at"`
	EstimatedDurationHours    int               `json:"estimated_duration_hours"`
	PriceRole                 string            `json:"price_role,omitempty"`
	DisplayAmountCent         int64             `json:"display_amount_cent"`
	RentAmountCent            *int64            `json:"rent_amount_cent,omitempty"`
	OwnerRentAmountCent       *int64            `json:"owner_rent_amount_cent,omitempty"`
	DepositAmountCent         int64             `json:"deposit_amount_cent"`
	DepositOriginalAmountCent int64             `json:"deposit_original_amount_cent"`
	DepositWaivedAmountCent   int64             `json:"deposit_waived_amount_cent"`
	PlatformFeeCent           *int64            `json:"platform_fee_cent,omitempty"`
	AccountSnapshot           datatypes.JSON    `json:"account_snapshot"`
	Status                    string            `json:"status"`
	HandoffStatus             string            `json:"handoff_status"`
	SettlementStatus          string            `json:"settlement_status"`
	ActiveDispute             *ActiveDisputeDTO `json:"active_dispute,omitempty"`
	Checkout                  *CheckoutDTO      `json:"checkout,omitempty"`
	PaymentDeadlineAt         *time.Time        `json:"payment_deadline_at,omitempty"`
	CreatedAt                 time.Time         `json:"created_at"`
	UpdatedAt                 time.Time         `json:"updated_at"`
}

type ActiveDisputeDTO struct {
	ID          uint64 `json:"id"`
	InitiatorID uint64 `json:"initiator_id"`
	Type        string `json:"type"`
	Status      string `json:"status"`
}

type CreateRequest struct {
	ListingID uint64 `json:"listing_id" binding:"required"`
}

type SubmitHandoffRequest struct {
	Content string `json:"content" binding:"required"`
}

type SubmitReturnRequest struct {
	Content string `json:"content" binding:"required"`
}

type SubmitCheckoutRequest struct {
	Content              string   `json:"content" binding:"required"`
	ConsumableAmountCent int64    `json:"consumable_amount_cent"`
	CoinConsumedM        float64  `json:"coin_consumed_m"`
	OtherAmountCent      int64    `json:"other_amount_cent"`
	EvidenceURLS         []string `json:"evidence_urls"`
}

type CounterCheckoutRequest struct {
	ConsumableAmountCent    int64    `json:"consumable_amount_cent"`
	CoinConsumedM           float64  `json:"coin_consumed_m"`
	OtherAmountCent         int64    `json:"other_amount_cent"`
	DepositDeductAmountCent int64    `json:"deposit_deduct_amount_cent"`
	Reason                  string   `json:"reason" binding:"required"`
	EvidenceURLS            []string `json:"evidence_urls"`
}

type AdminActionRequest struct {
	Reason string `json:"reason" binding:"required"`
}

type AuditMeta = auditlog.Meta

type PaginatedResult struct {
	Items    []OrderDTO `json:"items"`
	Total    int64      `json:"total"`
	Page     int        `json:"page"`
	PageSize int        `json:"page_size"`
}

type RefundStatusDTO struct {
	OrderID          uint64     `json:"order_id"`
	OrderNo          string     `json:"order_no"`
	RefundStatus     string     `json:"refund_status"`
	RefundAmountCent int64      `json:"refund_amount_cent"`
	RefundedAt       *time.Time `json:"refunded_at,omitempty"`
	TotalAmountCent  int64      `json:"total_amount_cent"`
}

type HandoffRecordDTO struct {
	ID                  uint64     `json:"id"`
	OrderID             uint64     `json:"order_id"`
	FromUserID          uint64     `json:"from_user_id"`
	ToUserID            uint64     `json:"to_user_id"`
	Type                string     `json:"type"`
	Content             string     `json:"content"`
	ConfirmedByRenterAt *time.Time `json:"confirmed_by_renter_at"`
	ConfirmedByOwnerAt  *time.Time `json:"confirmed_by_owner_at"`
	CreatedAt           time.Time  `json:"created_at"`
}

type CheckoutDTO struct {
	ID                      uint64     `json:"id"`
	OrderID                 uint64     `json:"order_id"`
	InitiatedBy             uint64     `json:"initiated_by"`
	Status                  string     `json:"status"`
	PriceRole               string     `json:"price_role,omitempty"`
	DisplayAmountCent       int64      `json:"display_amount_cent"`
	RentAmountCent          *int64     `json:"rent_amount_cent,omitempty"`
	OwnerRentAmountCent     *int64     `json:"owner_rent_amount_cent,omitempty"`
	PlatformFeeCent         *int64     `json:"platform_fee_cent,omitempty"`
	DepositAmountCent       int64      `json:"deposit_amount_cent"`
	ConsumableAmountCent    int64      `json:"consumable_amount_cent"`
	CoinConsumedM           float64    `json:"coin_consumed_m"`
	OtherAmountCent         int64      `json:"other_amount_cent"`
	DepositDeductAmountCent int64      `json:"deposit_deduct_amount_cent"`
	RenterRefundAmountCent  *int64     `json:"renter_refund_amount_cent,omitempty"`
	OwnerIncomeAmountCent   *int64     `json:"owner_income_amount_cent,omitempty"`
	Content                 string     `json:"content"`
	EvidenceURLS            []string   `json:"evidence_urls"`
	OwnerAdjustmentReason   string     `json:"owner_adjustment_reason"`
	OwnerAdjustedAt         *time.Time `json:"owner_adjusted_at"`
	RenterConfirmedAt       *time.Time `json:"renter_confirmed_at"`
	RenterRejectedAt        *time.Time `json:"renter_rejected_at"`
	CreatedAt               time.Time  `json:"created_at"`
	UpdatedAt               time.Time  `json:"updated_at"`
}
