package entities

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type PaymentOrderItem struct {
	ID          uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	PaymentID   uuid.UUID `gorm:"type:uuid;not null;index" json:"payment_id"`
	OrderItemID uuid.UUID `gorm:"type:uuid;not null;index" json:"order_item_id"`
	Quantity    int       `gorm:"not null;default:0" json:"quantity"` // Quantity paid for this specific payment
	Amount      float64   `gorm:"type:decimal(10,2);not null" json:"amount"`
	CreatedAt   time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt   time.Time `gorm:"autoUpdateTime" json:"updated_at"`

	Payment   Payment   `gorm:"foreignKey:PaymentID" json:"payment,omitempty"`
	OrderItem OrderItem `gorm:"foreignKey:OrderItemID" json:"order_item,omitempty"`
}

func (poi *PaymentOrderItem) BeforeCreate(tx *gorm.DB) error {
	if poi.ID == uuid.Nil {
		poi.ID = uuid.New()
	}
	return nil
}

func (PaymentOrderItem) TableName() string {
	return "payment_order_items"
}
