package entities

import (
	"time"

	"github.com/google/uuid"

	"gorm.io/gorm"
)

type PurchaseOrder struct {
	ID              uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	OrganizationID  uuid.UUID `gorm:"type:uuid;not null" json:"organization_id" validate:"required"`
	VendorID        uuid.UUID `gorm:"type:uuid;not null" json:"vendor_id" validate:"required"`
	PONumber        string    `gorm:"not null;size:50" json:"po_number" validate:"required,min=1,max=50"`
	TransactionDate time.Time `gorm:"type:date;not null" json:"transaction_date" validate:"required"`
	DueDate         time.Time `gorm:"type:date;not null" json:"due_date" validate:"required"`
	Reference       *string   `gorm:"size:100" json:"reference" validate:"omitempty,max=100"`
	Status          string    `gorm:"not null;size:20;default:'draft'" json:"status" validate:"required,oneof=draft sent approved received cancelled"`
	Message         *string   `gorm:"type:text" json:"message" validate:"omitempty"`
	TotalAmount     float64   `gorm:"type:decimal(15,2);not null;default:0" json:"total_amount"`
	CreatedAt       time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt       time.Time `gorm:"autoUpdateTime" json:"updated_at"`

	Organization *Organization             `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
	Vendor       *Vendor                   `gorm:"foreignKey:VendorID" json:"vendor,omitempty"`
	Items        []PurchaseOrderItem       `gorm:"foreignKey:PurchaseOrderID" json:"items,omitempty"`
	Attachments  []PurchaseOrderAttachment `gorm:"foreignKey:PurchaseOrderID" json:"attachments,omitempty"`
}

func (po *PurchaseOrder) BeforeCreate(tx *gorm.DB) error {
	if po.ID == uuid.Nil {
		id := uuid.New()
		po.ID = id
	}
	return nil
}

func (PurchaseOrder) TableName() string {
	return "purchase_orders"
}

type PurchaseOrderItem struct {
	ID              uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	PurchaseOrderID uuid.UUID `gorm:"type:uuid;not null" json:"purchase_order_id" validate:"required"`
	IngredientID    uuid.UUID `gorm:"type:uuid;not null" json:"ingredient_id" validate:"required"`
	Description     *string   `gorm:"type:text" json:"description" validate:"omitempty"`
	Quantity        float64   `gorm:"type:decimal(10,3);not null" json:"quantity" validate:"required,gt=0"`
	UnitID          uuid.UUID `gorm:"type:uuid;not null" json:"unit_id" validate:"required"`
	Amount          float64   `gorm:"type:decimal(15,2);not null" json:"amount" validate:"required,gte=0"`
	CreatedAt       time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt       time.Time `gorm:"autoUpdateTime" json:"updated_at"`

	PurchaseOrder *PurchaseOrder `gorm:"foreignKey:PurchaseOrderID" json:"purchase_order,omitempty"`
	Ingredient    *Ingredient    `gorm:"foreignKey:IngredientID" json:"ingredient,omitempty"`
	Unit          *Unit          `gorm:"foreignKey:UnitID" json:"unit,omitempty"`
}

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

func (PurchaseOrderItem) TableName() string {
	return "purchase_order_items"
}

type PurchaseOrderAttachment struct {
	ID              uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	PurchaseOrderID uuid.UUID `gorm:"type:uuid;not null" json:"purchase_order_id" validate:"required"`
	FileID          uuid.UUID `gorm:"type:uuid;not null" json:"file_id" validate:"required"`
	CreatedAt       time.Time `gorm:"autoCreateTime" json:"created_at"`

	PurchaseOrder *PurchaseOrder `gorm:"foreignKey:PurchaseOrderID" json:"purchase_order,omitempty"`
	File          *File          `gorm:"foreignKey:FileID" json:"file,omitempty"`
}

func (poa *PurchaseOrderAttachment) BeforeCreate(tx *gorm.DB) error {
	if poa.ID == uuid.Nil {
		id := uuid.New()
		poa.ID = id
	}
	return nil
}

func (PurchaseOrderAttachment) TableName() string {
	return "purchase_order_attachments"
}
