package models

import (
	"time"

	"gorm.io/gorm"
)

type Order struct {
	ID               uint           `gorm:"primaryKey" json:"id"`
	UserID           uint           `gorm:"index;not null" json:"user_id"`
	SupplierID       *uint          `gorm:"index" json:"supplier_id"`
	Subtotal         float64        `gorm:"type:decimal(10,2);not null" json:"subtotal"`
	ShippingFee      float64        `gorm:"type:decimal(10,2);default:0" json:"shipping_fee"`
	ServiceFee       float64        `gorm:"type:decimal(10,2);default:0" json:"service_fee"`
	Tax              float64        `gorm:"type:decimal(10,2);default:0" json:"tax"`
	TotalAmount      float64        `gorm:"type:decimal(10,2);not null" json:"total_amount"`
	RefundAmount     *float64       `gorm:"type:decimal(10,2)" json:"refund_amount"`
	RefundStatus     string         `gorm:"size:20" json:"refund_status"`
	RefundReason     string         `json:"refund_reason"`
	Status           string         `gorm:"size:20;not null;default:'pending_payment'" json:"status"`
	ShippingAddressID *uint         `json:"shipping_address_id"`
	TrackingNumber   string         `gorm:"size:100" json:"tracking_number"`
	ShippingPhoto    string         `gorm:"type:text" json:"shipping_photo"`
	ExpressPhoto     string         `gorm:"type:text" json:"express_photo"`
	CustomsPhoto     string         `gorm:"type:text" json:"customs_photo"`
	PaymentMethod    string         `gorm:"size:50" json:"payment_method"`
	PaymentExpiresAt *time.Time     `json:"payment_expires_at,omitempty"`
	CreatedAt        time.Time      `json:"created_at"`
	UpdatedAt        time.Time      `json:"updated_at"`
	DeletedAt        gorm.DeletedAt `gorm:"index" json:"-"`
	OrderItems       []OrderItem    `json:"order_items,omitempty"`
	ShippingAddress  *Address       `json:"shipping_address,omitempty"`
	User             User           `json:"user,omitempty"`
}

func (Order) TableName() string {
	return "orders"
}

type OrderItem struct {
	ID        uint    `gorm:"primaryKey" json:"id"`
	OrderID   uint    `gorm:"index;not null" json:"order_id"`
	ProductID uint    `gorm:"index;not null" json:"product_id"`
	Quantity  int     `gorm:"not null" json:"quantity"`
	Price     float64 `gorm:"type:decimal(10,2);not null" json:"price"`
	Product   Product `json:"product,omitempty"`
}

func (OrderItem) TableName() string {
	return "order_items"
}

const (
	OrderStatusPendingPayment = "pending_payment"
	OrderStatusPendingConfirm = "pending_confirm"
	OrderStatusPendingShip    = "pending_ship"
	OrderStatusShipped        = "shipped"
	OrderStatusCompleted      = "completed"
	OrderStatusRefunding      = "refunding"
	OrderStatusRefunded       = "refunded"
	OrderStatusCancelled      = "cancelled"
)

const (
	RefundStatusNone       = ""
	RefundStatusPending    = "pending"
	RefundStatusApproved   = "approved"
	RefundStatusRejected   = "rejected"
	RefundStatusCompleted  = "completed"
)
