package validation

import "mime/multipart"

type PurchaseItemPayload struct {
	WarehouseID uint    `json:"warehouse_id" validate:"required,gt=0"`
	ProductID   uint    `json:"product_id" validate:"required,gt=0"`
	Quantity    float64 `json:"qty" validate:"required,gt=0"`
}

type CreatePurchaseRequest struct {
	SupplierID uint                  `json:"supplier_id" validate:"required,gt=0"`
	CreditTerm int                   `json:"credit_term" validate:"required,number,gte=0"`
	DueDate    *string               `json:"due_date,omitempty" validate:"omitempty,datetime=2006-01-02"`
	Notes      *string               `json:"notes" validate:"omitempty,max=500"`
	Items      []PurchaseItemPayload `json:"items" validate:"required,min=1,dive"`
}

type StaffPurchaseApprovalItem struct {
	PurchaseItemID uint `json:"purchase_item_id,omitempty" validate:"omitempty,gt=0"`
	// For new items (no purchase_item_id), product_id is required.
	ProductID   uint     `json:"product_id,omitempty" validate:"required_without=PurchaseItemID,omitempty,gt=0"`
	WarehouseID uint     `json:"warehouse_id,omitempty" validate:"required_without=PurchaseItemID,omitempty,gt=0"`
	Qty         *float64 `json:"qty,omitempty" validate:"required_without=PurchaseItemID,omitempty,gt=0"`
	Price       float64  `json:"price" validate:"required,gt=0"`
	TotalPrice  float64  `json:"total_price" validate:"required,gt=0"`
}

type ApproveStaffPurchaseRequest struct {
	Action string                      `json:"action" validate:"required,oneof=APPROVED REJECTED"`
	Items  []StaffPurchaseApprovalItem `json:"items" validate:"omitempty,min=1,dive"`
	Notes  *string                     `json:"notes,omitempty" validate:"omitempty,max=500"`
}

type ApproveManagerPurchaseRequest struct {
	Action string  `json:"action" validate:"required,oneof=APPROVED REJECTED"`
	Notes  *string `json:"notes,omitempty" validate:"omitempty,max=500"`
}

type ReceivePurchaseItemRequest struct {
	PurchaseItemID     uint     `form:"purchase_item_id" json:"purchase_item_id" validate:"required,gt=0"`
	WarehouseID        *uint    `form:"warehouse_id" json:"warehouse_id" validate:"omitempty,gt=0"`
	ReceivedDate       string   `form:"received_date" json:"received_date" validate:"required,datetime=2006-01-02"`
	ExpeditionVendorID *uint    `form:"expedition_vendor_id" json:"expedition_vendor_id,omitempty" validate:"omitempty,gt=0"`
	TransportPerItem   *float64 `form:"transport_per_item" json:"transport_per_item,omitempty" validate:"omitempty,gte=0"`
	TravelNumber       *string  `form:"travel_number" json:"travel_number" validate:"omitempty,max=100"`
	TravelDocumentPath *string  `form:"travel_document_path" json:"travel_document_path" validate:"omitempty,max=1024"`
	VehicleNumber      *string  `form:"vehicle_number" json:"vehicle_number" validate:"omitempty,max=100"`
	ReceivedQty        *float64 `form:"received_qty" json:"received_qty" validate:"omitempty,gte=0"`
}

type ReceivePurchaseRequest struct {
	Action          string                       `form:"action" json:"action" validate:"required,oneof=APPROVED REJECTED"`
	Items           []ReceivePurchaseItemRequest `form:"items" json:"items" validate:"omitempty,dive"`
	TravelDocuments []*multipart.FileHeader      `form:"travel_documents" json:"-" validate:"omitempty,dive"`
	Notes           *string                      `form:"notes" json:"notes,omitempty" validate:"omitempty,max=500"`
}

type DeletePurchaseItemsRequest struct {
	ItemIDs []uint `json:"item_ids" validate:"required,min=1,dive,gt=0"`
}

type Query struct {
	Page                  int    `query:"page" validate:"omitempty,number,min=1"`
	Limit                 int    `query:"limit" validate:"omitempty,number,min=1,max=100"`
	SupplierID            uint   `query:"supplier_id" validate:"omitempty,gt=0"`
	AreaID                uint   `query:"area_id" validate:"omitempty,gt=0"`
	LocationID            uint   `query:"location_id" validate:"omitempty,gt=0"`
	ProjectFlockID        uint   `query:"project_flock_id" validate:"omitempty,gt=0"`
	ProjectFlockKandangID uint   `query:"project_flock_kandang_id" validate:"omitempty,gt=0"`
	ProductCategoryID     string `query:"product_category_id" validate:"omitempty,max=500"`
	ApprovalStatus        string `query:"approval_status" validate:"omitempty,max=500"`
	PoDate                string `query:"po_date" validate:"omitempty,datetime=2006-01-02"`
	PoDateFrom            string `query:"po_date_from" validate:"omitempty,datetime=2006-01-02"`
	PoDateTo              string `query:"po_date_to" validate:"omitempty,datetime=2006-01-02"`
	Search                string `query:"search" validate:"omitempty,max=100"`
	CreatedFrom           string `query:"created_from" validate:"omitempty,datetime=2006-01-02"`
	CreatedTo             string `query:"created_to" validate:"omitempty,datetime=2006-01-02"`
}
