package mysqlquerybuilder

import (
	"fmt"
	"strings"
)

// TODO: implementation more complete search service.

func BuildGetSearchQuery(baseQuery, joinQuery string, filter map[string]interface{}) (string, []any) {
	var conditions []string
	var args []any
	for key, value := range filter {
		if key == "id" {
			conditions = append(conditions, fmt.Sprintf("%s = ?", key))
			args = append(args, value)
		} else {
			conditions = append(conditions, fmt.Sprintf("%s LIKE ?", key))
			args = append(args, "%"+value.(string)+"%")
		}
	}

	query := baseQuery
	if len(conditions) > 0 {
		subQuery := strings.Join(conditions, " OR ")
		if joinQuery != "" {
			query += fmt.Sprintf(joinQuery, subQuery)
		} else {
			if strings.Contains(strings.ToUpper(baseQuery), "WHERE") {
				query += " AND "
			} else {
				query += " WHERE "
			}
			query += subQuery
		}
	}

	return query, args
}
