package models

import (
	"time"

	"github.com/google/uuid"
)

type PurchaseOrder struct {
	ID              uuid.UUID `json:"id"`
	OrganizationID  uuid.UUID `json:"organization_id"`
	VendorID        uuid.UUID `json:"vendor_id"`
	PONumber        string    `json:"po_number"`
	TransactionDate time.Time `json:"transaction_date"`
	DueDate         time.Time `json:"due_date"`
	Reference       *string   `json:"reference"`
	Status          string    `json:"status"`
	Message         *string   `json:"message"`
	TotalAmount     float64   `json:"total_amount"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

type PurchaseOrderItem struct {
	ID              uuid.UUID `json:"id"`
	PurchaseOrderID uuid.UUID `json:"purchase_order_id"`
	IngredientID    uuid.UUID `json:"ingredient_id"`
	Description     *string   `json:"description"`
	Quantity        float64   `json:"quantity"`
	UnitID          uuid.UUID `json:"unit_id"`
	Amount          float64   `json:"amount"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

type PurchaseOrderAttachment struct {
	ID              uuid.UUID `json:"id"`
	PurchaseOrderID uuid.UUID `json:"purchase_order_id"`
	FileID          uuid.UUID `json:"file_id"`
	CreatedAt       time.Time `json:"created_at"`
}

type PurchaseOrderResponse struct {
	ID              uuid.UUID                         `json:"id"`
	OrganizationID  uuid.UUID                         `json:"organization_id"`
	VendorID        uuid.UUID                         `json:"vendor_id"`
	PONumber        string                            `json:"po_number"`
	TransactionDate time.Time                         `json:"transaction_date"`
	DueDate         time.Time                         `json:"due_date"`
	Reference       *string                           `json:"reference"`
	Status          string                            `json:"status"`
	Message         *string                           `json:"message"`
	TotalAmount     float64                           `json:"total_amount"`
	CreatedAt       time.Time                         `json:"created_at"`
	UpdatedAt       time.Time                         `json:"updated_at"`
	Vendor          *VendorResponse                   `json:"vendor,omitempty"`
	Items           []PurchaseOrderItemResponse       `json:"items,omitempty"`
	Attachments     []PurchaseOrderAttachmentResponse `json:"attachments,omitempty"`
}

type PurchaseOrderItemResponse struct {
	ID              uuid.UUID           `json:"id"`
	PurchaseOrderID uuid.UUID           `json:"purchase_order_id"`
	IngredientID    uuid.UUID           `json:"ingredient_id"`
	Description     *string             `json:"description"`
	Quantity        float64             `json:"quantity"`
	UnitID          uuid.UUID           `json:"unit_id"`
	Amount          float64             `json:"amount"`
	CreatedAt       time.Time           `json:"created_at"`
	UpdatedAt       time.Time           `json:"updated_at"`
	Ingredient      *IngredientResponse `json:"ingredient,omitempty"`
	Unit            *UnitResponse       `json:"unit,omitempty"`
}

type PurchaseOrderAttachmentResponse struct {
	ID              uuid.UUID     `json:"id"`
	PurchaseOrderID uuid.UUID     `json:"purchase_order_id"`
	FileID          uuid.UUID     `json:"file_id"`
	CreatedAt       time.Time     `json:"created_at"`
	File            *FileResponse `json:"file,omitempty"`
}

type CreatePurchaseOrderRequest struct {
	VendorID          uuid.UUID                        `json:"vendor_id"`
	PONumber          string                           `json:"po_number"`
	TransactionDate   time.Time                        `json:"transaction_date"`
	DueDate           time.Time                        `json:"due_date"`
	Reference         *string                          `json:"reference,omitempty"`
	Status            *string                          `json:"status,omitempty"`
	Message           *string                          `json:"message,omitempty"`
	Items             []CreatePurchaseOrderItemRequest `json:"items"`
	AttachmentFileIDs []uuid.UUID                      `json:"attachment_file_ids,omitempty"`
}

type CreatePurchaseOrderItemRequest struct {
	IngredientID uuid.UUID `json:"ingredient_id"`
	Description  *string   `json:"description,omitempty"`
	Quantity     float64   `json:"quantity"`
	UnitID       uuid.UUID `json:"unit_id"`
	Amount       float64   `json:"amount"`
}

type UpdatePurchaseOrderRequest struct {
	VendorID          *uuid.UUID                       `json:"vendor_id,omitempty"`
	PONumber          *string                          `json:"po_number,omitempty"`
	TransactionDate   *time.Time                       `json:"transaction_date,omitempty"`
	DueDate           *time.Time                       `json:"due_date,omitempty"`
	Reference         *string                          `json:"reference,omitempty"`
	Status            *string                          `json:"status,omitempty"`
	Message           *string                          `json:"message,omitempty"`
	Items             []UpdatePurchaseOrderItemRequest `json:"items,omitempty"`
	AttachmentFileIDs []uuid.UUID                      `json:"attachment_file_ids,omitempty"`
}

type UpdatePurchaseOrderItemRequest struct {
	ID           *uuid.UUID `json:"id,omitempty"` // For existing items
	IngredientID *uuid.UUID `json:"ingredient_id,omitempty"`
	Description  *string    `json:"description,omitempty"`
	Quantity     *float64   `json:"quantity,omitempty"`
	UnitID       *uuid.UUID `json:"unit_id,omitempty"`
	Amount       *float64   `json:"amount,omitempty"`
}

type ListPurchaseOrdersRequest struct {
	Page      int        `json:"page" validate:"min=1"`
	Limit     int        `json:"limit" validate:"min=1,max=100"`
	Search    string     `json:"search,omitempty"`
	Status    string     `json:"status,omitempty"`
	VendorID  *uuid.UUID `json:"vendor_id,omitempty"`
	StartDate *time.Time `json:"start_date,omitempty"`
	EndDate   *time.Time `json:"end_date,omitempty"`
}

type ListPurchaseOrdersResponse struct {
	PurchaseOrders []PurchaseOrderResponse `json:"purchase_orders"`
	TotalCount     int                     `json:"total_count"`
	Page           int                     `json:"page"`
	Limit          int                     `json:"limit"`
	TotalPages     int                     `json:"total_pages"`
}
