package transformer

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

func CreateUserRequestToModel(req *contract.CreateUserRequest) *models.CreateUserRequest {
	return &models.CreateUserRequest{
		OrganizationID: req.OrganizationID,
		OutletID:       req.OutletID,
		Name:           req.Name,
		Email:          req.Email,
		Password:       req.Password,
		Role:           constants.UserRole(req.Role),
		Permissions:    req.Permissions,
	}
}

func UpdateUserRequestToModel(req *contract.UpdateUserRequest) *models.UpdateUserRequest {
	modelReq := &models.UpdateUserRequest{
		OutletID: req.OutletID,
		IsActive: req.IsActive,
	}

	if req.Name != nil {
		modelReq.Name = req.Name
	}

	if req.Email != nil {
		modelReq.Email = req.Email
	}

	if req.Role != nil {
		role := constants.UserRole(*req.Role)
		modelReq.Role = &role
	}

	if req.Permissions != nil {
		modelReq.Permissions = req.Permissions
	}

	return modelReq
}

func ChangePasswordRequestToModel(req *contract.ChangePasswordRequest) *models.ChangePasswordRequest {
	return &models.ChangePasswordRequest{
		CurrentPassword: req.CurrentPassword,
		NewPassword:     req.NewPassword,
	}
}

func UpdateUserOutletRequestToModel(req *contract.UpdateUserOutletRequest) *models.UpdateUserOutletRequest {
	return &models.UpdateUserOutletRequest{
		OutletID: req.OutletID,
	}
}

// Model to Contract conversions
func UserModelToResponse(user *models.User) *contract.UserResponse {
	return &contract.UserResponse{
		ID:             user.ID,
		OrganizationID: user.OrganizationID,
		OutletID:       user.OutletID,
		Name:           user.Name,
		Email:          user.Email,
		Role:           string(user.Role),
		Permissions:    user.Permissions,
		IsActive:       user.IsActive,
		CreatedAt:      user.CreatedAt,
		UpdatedAt:      user.UpdatedAt,
	}
}

func UserModelResponseToResponse(userResponse *models.UserResponse) *contract.UserResponse {
	return &contract.UserResponse{
		ID:             userResponse.ID,
		OrganizationID: userResponse.OrganizationID,
		OutletID:       userResponse.OutletID,
		Name:           userResponse.Name,
		Email:          userResponse.Email,
		Role:           string(userResponse.Role),
		Permissions:    userResponse.Permissions,
		IsActive:       userResponse.IsActive,
		CreatedAt:      userResponse.CreatedAt,
		UpdatedAt:      userResponse.UpdatedAt,
	}
}

func UsersToResponses(users []models.User) []contract.UserResponse {
	responses := make([]contract.UserResponse, len(users))
	for i, user := range users {
		response := UserModelToResponse(&user)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}

func UserResponsesToResponses(userResponses []models.UserResponse) []contract.UserResponse {
	responses := make([]contract.UserResponse, len(userResponses))
	for i, userResponse := range userResponses {
		response := UserModelResponseToResponse(&userResponse)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}
