package transformer

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

func CreateCategoryRequestToModel(apctx *appcontext.ContextInfo, req *contract.CreateCategoryRequest) *models.CreateCategoryRequest {
	return &models.CreateCategoryRequest{
		OrganizationID: apctx.OrganizationID,
		Name:           req.Name,
		Description:    req.Description,
		ImageURL:       nil,
		SortOrder:      0,
	}
}

func UpdateCategoryRequestToModel(req *contract.UpdateCategoryRequest) *models.UpdateCategoryRequest {
	return &models.UpdateCategoryRequest{
		Name:        req.Name,
		Description: req.Description,
		ImageURL:    nil,
		SortOrder:   nil,
		IsActive:    nil,
	}
}

func CategoryModelResponseToResponse(cat *models.CategoryResponse) *contract.CategoryResponse {
	if cat == nil {
		return nil
	}

	return &contract.CategoryResponse{
		ID:             cat.ID,
		OrganizationID: cat.OrganizationID,
		Name:           cat.Name,
		Description:    cat.Description,
		BusinessType:   "restaurant", // Default business type
		Metadata:       map[string]interface{}{},
		CreatedAt:      cat.CreatedAt,
		UpdatedAt:      cat.UpdatedAt,
	}
}

func CategoriesToResponses(categories []models.CategoryResponse) []contract.CategoryResponse {
	responses := make([]contract.CategoryResponse, len(categories))
	for i, cat := range categories {
		response := CategoryModelResponseToResponse(&cat)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}
