package invoices

import (
	"time"

	"rmser/internal/domain/catalog"

	"github.com/google/uuid"
	"github.com/shopspring/decimal"
)

// Invoice - Приходная накладная
type Invoice struct {
	ID             uuid.UUID `gorm:"type:uuid;primary_key;"`
	DocumentNumber string    `gorm:"type:varchar(100);index"`
	DateIncoming   time.Time `gorm:"index"`
	SupplierID     uuid.UUID `gorm:"type:uuid;index"`
	DefaultStoreID uuid.UUID `gorm:"type:uuid;index"`
	Status         string    `gorm:"type:varchar(50)"`

	Items []InvoiceItem `gorm:"foreignKey:InvoiceID;constraint:OnDelete:CASCADE"`

	CreatedAt time.Time
	UpdatedAt time.Time
}

// InvoiceItem - Позиция накладной
type InvoiceItem struct {
	ID        uuid.UUID       `gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	InvoiceID uuid.UUID       `gorm:"type:uuid;not null;index"`
	ProductID uuid.UUID       `gorm:"type:uuid;not null"`
	Amount    decimal.Decimal `gorm:"type:numeric(19,4);not null"`
	Price     decimal.Decimal `gorm:"type:numeric(19,4);not null"`
	Sum       decimal.Decimal `gorm:"type:numeric(19,4);not null"`
	VatSum    decimal.Decimal `gorm:"type:numeric(19,4)"`

	Product catalog.Product `gorm:"foreignKey:ProductID"`
}

type Repository interface {
	GetLastInvoiceDate() (*time.Time, error)
	SaveInvoices(invoices []Invoice) error
}
