package mappers

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

func CategoryEntityToModel(entity *entities.Category) *models.Category {
	if entity == nil {
		return nil
	}

	return &models.Category{
		ID:             entity.ID,
		OrganizationID: entity.OrganizationID,
		Name:           entity.Name,
		Description:    entity.Description,
		ImageURL:       nil,  // Entity doesn't have ImageURL, model does
		SortOrder:      0,    // Entity doesn't have SortOrder, model does
		IsActive:       true, // Entity doesn't have IsActive, default to true
		CreatedAt:      entity.CreatedAt,
		UpdatedAt:      entity.UpdatedAt,
	}
}

func CategoryModelToEntity(model *models.Category) *entities.Category {
	if model == nil {
		return nil
	}

	metadata := entities.Metadata{}
	if model.ImageURL != nil {
		metadata["image_url"] = *model.ImageURL
	}
	metadata["sort_order"] = model.SortOrder

	return &entities.Category{
		ID:             model.ID,
		OrganizationID: model.OrganizationID,
		Name:           model.Name,
		Description:    model.Description,
		BusinessType:   "restaurant", // Default business type
		Metadata:       metadata,
		CreatedAt:      model.CreatedAt,
		UpdatedAt:      model.UpdatedAt,
	}
}

func CreateCategoryRequestToEntity(req *models.CreateCategoryRequest) *entities.Category {
	if req == nil {
		return nil
	}

	metadata := entities.Metadata{}
	if req.ImageURL != nil {
		metadata["image_url"] = *req.ImageURL
	}
	metadata["sort_order"] = req.SortOrder

	return &entities.Category{
		OrganizationID: req.OrganizationID,
		Name:           req.Name,
		Description:    req.Description,
		BusinessType:   "restaurant", // Default business type
		Metadata:       metadata,
	}
}

func CategoryEntityToResponse(entity *entities.Category) *models.CategoryResponse {
	if entity == nil {
		return nil
	}

	// Extract image URL and sort order from metadata
	var imageURL *string
	var sortOrder int

	if entity.Metadata != nil {
		if imgURL, exists := entity.Metadata["image_url"]; exists {
			if imgURLStr, ok := imgURL.(string); ok {
				imageURL = &imgURLStr
			}
		}
		if sort, exists := entity.Metadata["sort_order"]; exists {
			if sortInt, ok := sort.(int); ok {
				sortOrder = sortInt
			} else if sortFloat, ok := sort.(float64); ok {
				sortOrder = int(sortFloat)
			}
		}
	}

	return &models.CategoryResponse{
		ID:             entity.ID,
		OrganizationID: entity.OrganizationID,
		Name:           entity.Name,
		Description:    entity.Description,
		ImageURL:       imageURL,
		SortOrder:      sortOrder,
		IsActive:       true, // Default to true since entity doesn't have this field
		CreatedAt:      entity.CreatedAt,
		UpdatedAt:      entity.UpdatedAt,
	}
}

func UpdateCategoryEntityFromRequest(entity *entities.Category, req *models.UpdateCategoryRequest) {
	if entity == nil || req == nil {
		return
	}

	if req.Name != nil {
		entity.Name = *req.Name
	}

	if req.Description != nil {
		entity.Description = req.Description
	}

	if entity.Metadata == nil {
		entity.Metadata = make(entities.Metadata)
	}

	if req.ImageURL != nil {
		entity.Metadata["image_url"] = *req.ImageURL
	}

	if req.SortOrder != nil {
		entity.Metadata["sort_order"] = *req.SortOrder
	}
}

func CategoryEntitiesToModels(entities []*entities.Category) []*models.Category {
	if entities == nil {
		return nil
	}

	models := make([]*models.Category, len(entities))
	for i, entity := range entities {
		models[i] = CategoryEntityToModel(entity)
	}
	return models
}

func CategoryEntitiesToResponses(entities []*entities.Category) []*models.CategoryResponse {
	if entities == nil {
		return nil
	}

	responses := make([]*models.CategoryResponse, len(entities))
	for i, entity := range entities {
		responses[i] = CategoryEntityToResponse(entity)
	}
	return responses
}
