package customer

import (
	"fmt"
	"net/http"
	"strconv"
	"strings"

	"dev.tandem.ws/tandem/camper/pkg/auth"
	"dev.tandem.ws/tandem/camper/pkg/form"
)

type filterForm struct {
	company *auth.Company
	Name    *form.Input
	Email   *form.Input
	Cursor  *form.Cursor
}

func newFilterForm(company *auth.Company) *filterForm {
	return &filterForm{
		company: company,
		Name: &form.Input{
			Name: "name",
		},
		Email: &form.Input{
			Name: "email",
		},
		Cursor: &form.Cursor{
			Name:    "cursor",
			PerPage: 25,
		},
	}
}

func (f *filterForm) Parse(r *http.Request) error {
	if err := r.ParseForm(); err != nil {
		return err
	}
	f.Name.FillValue(r)
	f.Email.FillValue(r)
	f.Cursor.FillValue(r)
	return nil
}

func (f *filterForm) BuildQuery(args []interface{}) (string, []interface{}) {
	var where []string
	appendWhere := func(expression string, value interface{}) {
		args = append(args, value)
		where = append(where, fmt.Sprintf(expression, len(args)))
	}
	maybeAppendWhere := func(expression string, value string, conv func(string) interface{}) {
		if value != "" {
			if conv == nil {
				appendWhere(expression, value)
			} else {
				appendWhere(expression, conv(value))
			}
		}
	}

	appendWhere("company_id = $%d", f.company.ID)
	maybeAppendWhere("name ILIKE $%d", f.Name.Val, func(v string) interface{} {
		return "%" + v + "%"
	})
	maybeAppendWhere("email ILIKE $%d", f.Email.Val, func(v string) interface{} {
		return "%" + v + "%"
	})

	if f.Paginated() {
		params := f.Cursor.Params()
		if len(params) == 2 {
			where = append(where, fmt.Sprintf("(name, contact_id) > ($%d, $%d)", len(args)+1, len(args)+2))
			args = append(args, params[0])
			args = append(args, params[1])
		}
	}

	return strings.Join(where, ") AND ("), args
}

func (f *filterForm) buildCursor(customers []*customerEntry) []*customerEntry {
	return form.BuildCursor(f.Cursor, customers, func(entry *customerEntry) []string {
		return []string{entry.Name, strconv.Itoa(entry.ID)}
	})
}

func (f *filterForm) HasValue() bool {
	return f.Name.Val != "" ||
		f.Email.Val != ""
}

func (f *filterForm) PerPage() int {
	return f.Cursor.PerPage
}

func (f *filterForm) Paginated() bool {
	return f.Cursor.Pagination
}
