package contract

import (
	"time"

	"github.com/google/uuid"
)

type CreateInventoryRequest struct {
	OutletID     uuid.UUID `json:"outlet_id" validate:"required"`
	ProductID    uuid.UUID `json:"product_id" validate:"required"`
	Quantity     int       `json:"quantity" validate:"min=0"`
	ReorderLevel int       `json:"reorder_level" validate:"min=0"`
}

type UpdateInventoryRequest struct {
	Quantity     *int `json:"quantity,omitempty" validate:"omitempty,min=0"`
	ReorderLevel *int `json:"reorder_level,omitempty" validate:"omitempty,min=0"`
}

type AdjustInventoryRequest struct {
	ProductID uuid.UUID `json:"product_id" validate:"required"`
	OutletID  uuid.UUID `json:"outlet_id" validate:"required"`
	Delta     int       `json:"delta" validate:"required"` // Can be positive or negative
	Reason    string    `json:"reason" validate:"required,min=1,max=255"`
}

type RestockInventoryRequest struct {
	OutletID uuid.UUID `json:"outlet_id" validate:"required"`
	Items    []RestockItem `json:"items" validate:"required,min=1,dive"`
	Reason   string    `json:"reason" validate:"required,min=1,max=255"`
}

type RestockItem struct {
	ItemID   uuid.UUID `json:"item_id" validate:"required"`
	ItemType string    `json:"item_type" validate:"required,oneof=PRODUCT INGREDIENT"`
	Quantity int       `json:"quantity" validate:"required,min=1"`
}

type ListInventoryRequest struct {
	OutletID      *uuid.UUID `json:"outlet_id,omitempty"`
	ProductID     *uuid.UUID `json:"product_id,omitempty"`
	CategoryID    *uuid.UUID `json:"category_id,omitempty"`
	LowStockOnly  *bool      `json:"low_stock_only,omitempty"`
	ZeroStockOnly *bool      `json:"zero_stock_only,omitempty"`
	Search        string     `json:"search,omitempty"`
	Page          int        `json:"page" validate:"required,min=1"`
	Limit         int        `json:"limit" validate:"required,min=1,max=100"`
}

type InventoryResponse struct {
	ID           uuid.UUID `json:"id"`
	OutletID     uuid.UUID `json:"outlet_id"`
	ProductID    uuid.UUID `json:"product_id"`
	Quantity     int       `json:"quantity"`
	ReorderLevel int       `json:"reorder_level"`
	IsLowStock   bool      `json:"is_low_stock"`
	UpdatedAt    time.Time `json:"updated_at"`

	// Related data (optional)
	Product *ProductResponse `json:"product,omitempty"`
	Outlet  *OutletResponse  `json:"outlet,omitempty"`
}

type ListInventoryResponse struct {
	Inventory  []InventoryResponse `json:"inventory"`
	TotalCount int                 `json:"total_count"`
	Page       int                 `json:"page"`
	Limit      int                 `json:"limit"`
	TotalPages int                 `json:"total_pages"`
}

type InventoryAdjustmentResponse struct {
	InventoryID uuid.UUID `json:"inventory_id"`
	ProductID   uuid.UUID `json:"product_id"`
	OutletID    uuid.UUID `json:"outlet_id"`
	PreviousQty int       `json:"previous_quantity"`
	NewQty      int       `json:"new_quantity"`
	Delta       int       `json:"delta"`
	Reason      string    `json:"reason"`
	AdjustedAt  time.Time `json:"adjusted_at"`
}

type RestockInventoryResponse struct {
	OutletID uuid.UUID `json:"outlet_id"`
	Items    []RestockItemResult `json:"items"`
	Reason   string    `json:"reason"`
	RestockedAt time.Time `json:"restocked_at"`
}

type RestockItemResult struct {
	ItemID      uuid.UUID `json:"item_id"`
	ItemType    string    `json:"item_type"`
	ItemName    string    `json:"item_name"`
	PreviousQty int       `json:"previous_quantity"`
	NewQty      int       `json:"new_quantity"`
	AddedQty    int       `json:"added_quantity"`
	Success     bool      `json:"success"`
	Error       string    `json:"error,omitempty"`
}

// Inventory Report Contracts
type InventoryReportSummaryResponse struct {
	TotalProducts        int     `json:"total_products"`
	TotalIngredients     int     `json:"total_ingredients"`
	TotalValue           float64 `json:"total_value"`
	LowStockProducts     int     `json:"low_stock_products"`
	LowStockIngredients  int     `json:"low_stock_ingredients"`
	ZeroStockProducts    int     `json:"zero_stock_products"`
	ZeroStockIngredients int     `json:"zero_stock_ingredients"`
	TotalSoldProducts    float64 `json:"total_sold_products"`
	TotalSoldIngredients float64 `json:"total_sold_ingredients"`
	OutletID             string  `json:"outlet_id"`
	OutletName           string  `json:"outlet_name"`
	GeneratedAt          string  `json:"generated_at"`
}

type InventoryReportDetailResponse struct {
	Summary     *InventoryReportSummaryResponse      `json:"summary"`
	Products    []*InventoryProductDetailResponse    `json:"products"`
	Ingredients []*InventoryIngredientDetailResponse `json:"ingredients"`
}

type InventoryProductDetailResponse struct {
	ID           string  `json:"id"`
	ProductID    string  `json:"product_id"`
	ProductName  string  `json:"product_name"`
	CategoryName string  `json:"category_name"`
	Quantity     int     `json:"quantity"`
	ReorderLevel int     `json:"reorder_level"`
	UnitCost     float64 `json:"unit_cost"`
	TotalValue   float64 `json:"total_value"`
	TotalIn      float64 `json:"total_in"`
	TotalOut     float64 `json:"total_out"`
	IsLowStock   bool    `json:"is_low_stock"`
	IsZeroStock  bool    `json:"is_zero_stock"`
	UpdatedAt    string  `json:"updated_at"`
}

type InventoryIngredientDetailResponse struct {
	ID             string  `json:"id"`
	IngredientID   string  `json:"ingredient_id"`
	IngredientName string  `json:"ingredient_name"`
	UnitName       string  `json:"unit_name"`
	Quantity       int     `json:"quantity"`
	ReorderLevel   int     `json:"reorder_level"`
	UnitCost       float64 `json:"unit_cost"`
	TotalValue     float64 `json:"total_value"`
	TotalIn        float64 `json:"total_in"`
	TotalOut       float64 `json:"total_out"`
	IsLowStock     bool    `json:"is_low_stock"`
	IsZeroStock    bool    `json:"is_zero_stock"`
	UpdatedAt      string  `json:"updated_at"`
}
