package models

import (
	"apskel-pos-be/internal/constants"
	"time"

	"github.com/google/uuid"
)

type Payment struct {
	ID               uuid.UUID
	OrderID          uuid.UUID
	PaymentMethodID  uuid.UUID
	Amount           float64
	Status           constants.PaymentTransactionStatus
	TransactionID    *string
	SplitNumber      int
	SplitTotal       int
	SplitType        *string
	SplitDescription *string
	RefundAmount     float64
	RefundReason     *string
	RefundedAt       *time.Time
	RefundedBy       *uuid.UUID
	Metadata         map[string]interface{}
	CreatedAt        time.Time
	UpdatedAt        time.Time
}

type CreatePaymentRequest struct {
	OrderID           uuid.UUID                       `validate:"required"`
	PaymentMethodID   uuid.UUID                       `validate:"required"`
	Amount            float64                         `validate:"required,min=0"`
	TransactionID     *string                         `validate:"omitempty"`
	SplitNumber       int                             `validate:"omitempty,min=1"`
	SplitTotal        int                             `validate:"omitempty,min=1"`
	SplitType         *string                         `validate:"omitempty,oneof=AMOUNT ITEM"`
	SplitDescription  *string                         `validate:"omitempty,max=255"`
	PaymentOrderItems []CreatePaymentOrderItemRequest `validate:"omitempty,dive"`
	Metadata          map[string]interface{}
}

type UpdatePaymentRequest struct {
	Status        *constants.PaymentTransactionStatus
	TransactionID *string
	Notes         *string `validate:"omitempty,max=1000"`
}

type PaymentResponse struct {
	ID                uuid.UUID
	OrderID           uuid.UUID
	PaymentMethodID   uuid.UUID
	PaymentMethodName string
	PaymentMethodType constants.PaymentMethodType
	Amount            float64
	Status            constants.PaymentTransactionStatus
	TransactionID     *string
	SplitNumber       int
	SplitTotal        int
	SplitType         *string
	SplitDescription  *string
	RefundAmount      float64
	RefundReason      *string
	RefundedAt        *time.Time
	RefundedBy        *uuid.UUID
	Metadata          map[string]interface{}
	CreatedAt         time.Time
	UpdatedAt         time.Time
	PaymentOrderItems []PaymentOrderItemResponse
}

func (p *Payment) IsSuccessful() bool {
	return p.Status == constants.PaymentTransactionStatusCompleted
}

func (p *Payment) IsPending() bool {
	return p.Status == constants.PaymentTransactionStatusPending
}

func (p *Payment) CanBeRefunded() bool {
	return p.Status == constants.PaymentTransactionStatusCompleted
}

func (p *Payment) IsProcessed() bool {
	return p.Status == constants.PaymentTransactionStatusCompleted || p.Status == constants.PaymentTransactionStatusFailed
}
