package mappers

import (
	"apskel-pos-be/internal/entities"
	"apskel-pos-be/internal/models"

	"github.com/google/uuid"
)

// ToCustomerResponse converts a customer entity to a customer response
func ToCustomerResponse(customer *entities.Customer) *models.CustomerResponse {
	if customer == nil {
		return nil
	}

	return &models.CustomerResponse{
		ID:             customer.ID,
		OrganizationID: customer.OrganizationID,
		Name:           customer.Name,
		Email:          customer.Email,
		Phone:          customer.Phone,
		Address:        customer.Address,
		IsDefault:      customer.IsDefault,
		IsActive:       customer.IsActive,
		Metadata:       customer.Metadata,
		CreatedAt:      customer.CreatedAt,
		UpdatedAt:      customer.UpdatedAt,
	}
}

// ToCustomerResponses converts a slice of customer entities to customer responses
func ToCustomerResponses(customers []entities.Customer) []models.CustomerResponse {
	responses := make([]models.CustomerResponse, len(customers))
	for i, customer := range customers {
		responses[i] = *ToCustomerResponse(&customer)
	}
	return responses
}

// ToCustomerEntity converts a create customer request to a customer entity
func ToCustomerEntity(req *models.CreateCustomerRequest, organizationID uuid.UUID) *entities.Customer {
	return &entities.Customer{
		OrganizationID: organizationID,
		Name:           req.Name,
		Email:          req.Email,
		Phone:          req.Phone,
		Address:        req.Address,
		IsDefault:      false, // New customers are not default by default
		IsActive:       true,
		Metadata:       entities.Metadata{},
	}
}

// UpdateCustomerEntity updates a customer entity with update request data
func UpdateCustomerEntity(customer *entities.Customer, req *models.UpdateCustomerRequest) {
	if req.Name != nil {
		customer.Name = *req.Name
	}
	if req.Email != nil {
		customer.Email = req.Email
	}
	if req.Phone != nil {
		customer.Phone = req.Phone
	}
	if req.Address != nil {
		customer.Address = req.Address
	}
	if req.IsActive != nil {
		customer.IsActive = *req.IsActive
	}
}
