package models

import (
	"time"

	"github.com/google/uuid"
)

type Inventory struct {
	ID           uuid.UUID
	OutletID     uuid.UUID
	ProductID    uuid.UUID
	Quantity     int
	ReorderLevel int
	UpdatedAt    time.Time
}

type CreateInventoryRequest struct {
	OutletID     uuid.UUID
	ProductID    uuid.UUID
	Quantity     int
	ReorderLevel int
}

type UpdateInventoryRequest struct {
	Quantity     *int
	ReorderLevel *int
}

type InventoryAdjustmentRequest struct {
	ProductID uuid.UUID
	OutletID  uuid.UUID
	Delta     int
	Reason    string
}

type InventoryResponse struct {
	ID           uuid.UUID
	OutletID     uuid.UUID
	ProductID    uuid.UUID
	ProductName  string
	Quantity     int
	ReorderLevel int
	IsLowStock   bool
	UpdatedAt    time.Time
}

func (i *Inventory) IsLowStock() bool {
	return i.Quantity <= i.ReorderLevel
}

func (i *Inventory) CanFulfillOrder(requestedQuantity int) bool {
	return i.Quantity >= requestedQuantity
}

func (i *Inventory) AdjustQuantity(delta int) int {
	newQuantity := i.Quantity + delta
	if newQuantity < 0 {
		newQuantity = 0
	}
	return newQuantity
}

// Inventory Report Models
type InventoryReportSummary 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             uuid.UUID `json:"outlet_id"`
	OutletName           string    `json:"outlet_name"`
	GeneratedAt          time.Time `json:"generated_at"`
}

type InventoryReportDetail struct {
	Summary     *InventoryReportSummary      `json:"summary"`
	Products    []*InventoryProductDetail    `json:"products"`
	Ingredients []*InventoryIngredientDetail `json:"ingredients"`
}

type InventoryProductDetail struct {
	ID           uuid.UUID `json:"id"`
	ProductID    uuid.UUID `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    time.Time `json:"updated_at"`
}

type InventoryIngredientDetail struct {
	ID             uuid.UUID `json:"id"`
	IngredientID   uuid.UUID `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      time.Time `json:"updated_at"`
}

type InventoryReportFilter struct {
	OutletID      *uuid.UUID `json:"outlet_id"`
	CategoryID    *uuid.UUID `json:"category_id"`
	ShowLowStock  *bool      `json:"show_low_stock"`
	ShowZeroStock *bool      `json:"show_zero_stock"`
	Search        *string    `json:"search"`
	Limit         *int       `json:"limit"`
	Offset        *int       `json:"offset"`
	DateFrom      *time.Time `json:"date_from"`
	DateTo        *time.Time `json:"date_to"`
}
