package transformer

import (
	"apskel-pos-be/internal/contract"
	"math"
)

func PaginationToRequest(page, limit int) (int, int) {
	if page < 1 {
		page = 1
	}
	if limit < 1 {
		limit = 10
	}
	if limit > 100 {
		limit = 100
	}
	return page, limit
}

func CreatePaginationResponse(totalCount, page, limit int) contract.PaginationResponse {
	totalPages := int(math.Ceil(float64(totalCount) / float64(limit)))
	if totalPages < 1 {
		totalPages = 1
	}

	return contract.PaginationResponse{
		TotalCount: totalCount,
		Page:       page,
		Limit:      limit,
		TotalPages: totalPages,
	}
}

func CreateListUsersResponse(users []contract.UserResponse, totalCount, page, limit int) *contract.ListUsersResponse {
	pagination := CreatePaginationResponse(totalCount, page, limit)
	return &contract.ListUsersResponse{
		Users:      users,
		Pagination: pagination,
	}
}

func CreateListOrganizationsResponse(orgs []contract.OrganizationResponse, totalCount, page, limit int) *contract.ListOrganizationsResponse {
	pagination := CreatePaginationResponse(totalCount, page, limit)
	return &contract.ListOrganizationsResponse{
		Organizations: orgs,
		TotalCount:    pagination.TotalCount,
		Page:          pagination.Page,
		Limit:         pagination.Limit,
		TotalPages:    pagination.TotalPages,
	}
}

func CreateListOutletsResponse(outlets []contract.OutletResponse, totalCount, page, limit int) *contract.ListOutletsResponse {
	pagination := CreatePaginationResponse(totalCount, page, limit)
	return &contract.ListOutletsResponse{
		Outlets:    outlets,
		TotalCount: pagination.TotalCount,
		Page:       pagination.Page,
		Limit:      pagination.Limit,
		TotalPages: pagination.TotalPages,
	}
}

func CreateListOrdersResponse(orders []contract.OrderResponse, totalCount, page, limit int) *contract.ListOrdersResponse {
	pagination := CreatePaginationResponse(totalCount, page, limit)
	return &contract.ListOrdersResponse{
		Orders:     orders,
		TotalCount: pagination.TotalCount,
		Page:       pagination.Page,
		Limit:      pagination.Limit,
		TotalPages: pagination.TotalPages,
	}
}

func CreateListProductsResponse(products []contract.ProductResponse, totalCount, page, limit int) *contract.ListProductsResponse {
	pagination := CreatePaginationResponse(totalCount, page, limit)
	return &contract.ListProductsResponse{
		Products:   products,
		TotalCount: pagination.TotalCount,
		Page:       pagination.Page,
		Limit:      pagination.Limit,
		TotalPages: pagination.TotalPages,
	}
}

func CreateErrorResponse(message string, code int) *contract.ErrorResponse {
	return &contract.ErrorResponse{
		Error:   "error",
		Message: message,
		Code:    code,
	}
}

func CreateValidationErrorResponse(message string, details map[string]string) *contract.ValidationErrorResponse {
	return &contract.ValidationErrorResponse{
		Error:   "validation_error",
		Message: message,
		Details: details,
		Code:    400,
	}
}

func CreateSuccessResponse(message string, data interface{}) *contract.SuccessResponse {
	return &contract.SuccessResponse{
		Message: message,
		Data:    data,
	}
}
