package entities

import (
	"time"

	"gorm.io/gorm"
)

type LayingTransfer struct {
	Id                          uint           `gorm:"primaryKey"`
	TransferNumber              string         `gorm:"uniqueIndex;not null"`
	FromProjectFlockId          uint           `gorm:"not null"`
	ToProjectFlockId            uint           `gorm:"not null"`
	SourceProjectFlockKandangId *uint          `gorm:"index"`
	SourceProductWarehouseId    *uint          `gorm:"index"`
	SourceRequestedQty          float64        `gorm:"type:numeric(15,3);default:0;not null"`
	SourceUsageQty              float64        `gorm:"type:numeric(15,3);default:0;not null"`
	SourcePendingUsageQty       float64        `gorm:"type:numeric(15,3);default:0;not null"`
	TransferDate                time.Time      `gorm:"type:date;not null"`
	EconomicCutoffDate          *time.Time     `gorm:"type:date"`
	EffectiveMoveDate           *time.Time     `gorm:"type:date"`
	ExecutedAt                  *time.Time     `gorm:"type:timestamptz"`
	ExecutedBy                  *uint          `gorm:"index"`
	Notes                       string         `gorm:"type:text"`
	CreatedBy                   uint           `gorm:"not null"`
	CreatedAt                   time.Time      `gorm:"autoCreateTime"`
	UpdatedAt                   time.Time      `gorm:"autoUpdateTime"`
	DeletedAt                   gorm.DeletedAt `gorm:"index"`

	FromProjectFlock          *ProjectFlock          `gorm:"foreignKey:FromProjectFlockId;references:Id"`
	ToProjectFlock            *ProjectFlock          `gorm:"foreignKey:ToProjectFlockId;references:Id"`
	SourceProjectFlockKandang *ProjectFlockKandang   `gorm:"foreignKey:SourceProjectFlockKandangId;references:Id"`
	SourceProductWarehouse    *ProductWarehouse      `gorm:"foreignKey:SourceProductWarehouseId;references:Id"`
	CreatedUser               *User                  `gorm:"foreignKey:CreatedBy;references:Id"`
	ExecutedUser              *User                  `gorm:"foreignKey:ExecutedBy;references:Id"`
	Sources                   []LayingTransferSource `gorm:"foreignKey:LayingTransferId;constraint:OnDelete:CASCADE"`
	Targets                   []LayingTransferTarget `gorm:"foreignKey:LayingTransferId;constraint:OnDelete:CASCADE"`
	LatestApproval            *Approval              `gorm:"-" json:"-"`
}
