package transformer

import (
	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/models"
	"time"
)

func CreateInventoryRequestToModel(req *contract.CreateInventoryRequest) *models.CreateInventoryRequest {
	return &models.CreateInventoryRequest{
		OutletID:     req.OutletID,
		ProductID:    req.ProductID,
		Quantity:     req.Quantity,
		ReorderLevel: req.ReorderLevel,
	}
}

func UpdateInventoryRequestToModel(req *contract.UpdateInventoryRequest) *models.UpdateInventoryRequest {
	return &models.UpdateInventoryRequest{
		Quantity:     req.Quantity,
		ReorderLevel: req.ReorderLevel,
	}
}

func AdjustInventoryRequestToModel(req *contract.AdjustInventoryRequest) *models.InventoryAdjustmentRequest {
	return &models.InventoryAdjustmentRequest{
		Delta:  req.Delta,
		Reason: req.Reason,
	}
}

// Model to Contract conversions
func InventoryModelResponseToResponse(inv *models.InventoryResponse) *contract.InventoryResponse {
	if inv == nil {
		return nil
	}

	return &contract.InventoryResponse{
		ID:           inv.ID,
		OutletID:     inv.OutletID,
		ProductID:    inv.ProductID,
		Quantity:     inv.Quantity,
		ReorderLevel: inv.ReorderLevel,
		IsLowStock:   inv.IsLowStock,
		UpdatedAt:    inv.UpdatedAt,
		Product: &contract.ProductResponse{
			ID:   inv.ProductID,
			Name: inv.ProductName,
		},
	}
}

func InventoryAdjustmentToResponse(
	inventoryID, productID, outletID string,
	previousQty, newQty, delta int,
	reason string,
) *contract.InventoryAdjustmentResponse {
	// This would need proper UUID parsing in a real implementation
	return &contract.InventoryAdjustmentResponse{
		PreviousQty: previousQty,
		NewQty:      newQty,
		Delta:       delta,
		Reason:      reason,
		AdjustedAt:  time.Now(),
	}
}

// Slice conversions
func InventoryToResponses(inventory []models.InventoryResponse) []contract.InventoryResponse {
	responses := make([]contract.InventoryResponse, len(inventory))
	for i, inv := range inventory {
		response := InventoryModelResponseToResponse(&inv)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}
