package model

import (
	"regexp"
	"strings"

	"gordenko.dev/dima/qx"
	"gordenko.dev/dima/textutil"
)

func New(qxdb *qx.Db) *Model {
	s := new(Model)
	s.qx = qxdb
	return s
}

var reCountryCode = regexp.MustCompile(`\([a-z]{3}\)`)

// NormalizeNameToAlias - удаляет лишние пробельные символы. Заменяет whitespace символы на пробел,
// приводит к нижнему регистру
func NormalizeNameToAlias(name string) string {
	name = strings.ToLower(name)

	// вырезаем код страны если есть (пример: (Aut))
	name = reCountryCode.ReplaceAllString(name, "")

	name = strings.TrimSpace(name)

	return textutil.NormalizeWhitespaces(name)
}

/*
func (s *Model) ListFootballChamps() (list []FootballChamp, err error) {
	err = s.qx.ListBy(&list, "ORDER BY name ASC")
	return
}


func (s *Model) ListFootballChampTeams(champID int64) (list []Team, err error) {
	err = s.qx.ListBy(&list, "WHERE champID=? ORDER BY canonicalName ASC", champID)
	return
}


func (s *Model) ListFootballTeamAliases(teamID int64) (list []Alias, err error) {
	err = s.qx.ListBy(&list, "WHERE teamID=? ORDER BY name ASC", teamID)
	return
}

func (s *Model) RemoveFootballTeam(teamID int64) (err error) {
	return s.qx.Exec("DELETE FROM footballTeams WHERE teamID=?", teamID)
}

func (s *Model) RemoveFootballAlias(aliasID int64) (err error) {
	return s.qx.Exec("DELETE FROM footballTeamAliases WHERE aliasID=?", aliasID)
}

func (s *Model) AddFootballAlias(req AddFootballAliasReq) (aliasID int64, err error) {
	if req.Name == "" {
		err = api.Err{
			Code:  EmptyValue,
			Field: "name",
		}
		return
	}

	tx, err := s.qx.Tx()
	if err != nil {
		return
	}
	defer tx.Close(&err)

	aliasID, err = tx.Insert(req)
	if err != nil {
		return
	}
	err = tx.Commit()
	return
}


*/
