package mysqlquerybuilder

import (
	"fmt"

	"git.gocasts.ir/ebhomengo/niki/param"
)

func BuildGetAllQuery(baseQuery, table string, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) (query string, args []any) {
	filterQuery, fArgs := BuildFilterQuery(baseQuery, table, filter)
	paginationQuery, pArgs := BuildPaginationQuery(pagination)
	sortQuery := BuildSortQuery(sort)

	args = append(args, fArgs...)
	args = append(args, pArgs...)

	query = filterQuery

	if sortQuery != "" {
		query = fmt.Sprintf("%s %s", query, sortQuery)
	}
	if paginationQuery != "" {
		query = fmt.Sprintf("%s %s", query, paginationQuery)
	}

	return query, args
}

func BuildGetTotalQuery(table, joinQuery string, filter param.FilterRequest, searchParams *param.QuerySearch, hasDeletedAt bool) (string, []any) {
	var sArgs []any

	baseQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s ", table)

	if searchParams != nil {
		baseQuery, sArgs = BuildGetSearchQuery(baseQuery, joinQuery, *searchParams)
	}
	query, fArgs := BuildGetAllQuery(baseQuery, table, filter, param.PaginationRequest{}, param.SortRequest{})
	if hasDeletedAt {
		query = BuildDeletedAtQuery(query, table)
	}
	args := append(sArgs, fArgs...)

	return query, args
}
