package contract

import (
	"time"

	"github.com/google/uuid"
)

type CreatePurchaseOrderRequest struct {
	VendorID          uuid.UUID                        `json:"vendor_id" validate:"required"`
	PONumber          string                           `json:"po_number" validate:"required,min=1,max=50"`
	TransactionDate   string                           `json:"transaction_date" validate:"required"` // Format: YYYY-MM-DD
	DueDate           string                           `json:"due_date" validate:"required"`          // Format: YYYY-MM-DD
	Reference         *string                          `json:"reference,omitempty" validate:"omitempty,max=100"`
	Status            *string                          `json:"status,omitempty" validate:"omitempty,oneof=draft sent approved received cancelled"`
	Message           *string                          `json:"message,omitempty" validate:"omitempty"`
	Items             []CreatePurchaseOrderItemRequest `json:"items" validate:"required,min=1,dive"`
	AttachmentFileIDs []uuid.UUID                      `json:"attachment_file_ids,omitempty"`
}

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

type UpdatePurchaseOrderRequest struct {
	VendorID          *uuid.UUID                       `json:"vendor_id,omitempty" validate:"omitempty"`
	PONumber          *string                          `json:"po_number,omitempty" validate:"omitempty,min=1,max=50"`
	TransactionDate   *string                          `json:"transaction_date,omitempty" validate:"omitempty"` // Format: YYYY-MM-DD
	DueDate           *string                          `json:"due_date,omitempty" validate:"omitempty"`          // Format: YYYY-MM-DD
	Reference         *string                          `json:"reference,omitempty" validate:"omitempty,max=100"`
	Status            *string                          `json:"status,omitempty" validate:"omitempty,oneof=draft sent approved received cancelled"`
	Message           *string                          `json:"message,omitempty" validate:"omitempty"`
	Items             []UpdatePurchaseOrderItemRequest `json:"items,omitempty" validate:"omitempty,dive"`
	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" validate:"omitempty"`
	Description  *string    `json:"description,omitempty" validate:"omitempty"`
	Quantity     *float64   `json:"quantity,omitempty" validate:"omitempty,gt=0"`
	UnitID       *uuid.UUID `json:"unit_id,omitempty" validate:"omitempty"`
	Amount       *float64   `json:"amount,omitempty" validate:"omitempty,gte=0"`
}

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 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" validate:"omitempty,oneof=draft sent approved received cancelled"`
	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"`
}

// Helper types for ingredient and unit responses
type IngredientResponse struct {
	ID   uuid.UUID `json:"id"`
	Name string    `json:"name"`
}

type UnitResponse struct {
	ID   uuid.UUID `json:"id"`
	Name string    `json:"name"`
}
