package tables

import (
	"Wine-Server/utils"
)

type ManagerTable struct {
	Id    string
	Super bool
	First utils.TimeType
	Last  utils.TimeType

	Name     string
	Account  string
	Password string

	Order  uint32
	Income uint64
}

func CreateManagerTable() error {
	sql := "CREATE TABLE IF NOT EXISTS `manager`(" +
		"`id` VARCHAR(16) PRIMARY KEY," +
		"`super` BOOL DEFAULT FALSE," +
		"`first` DATETIME DEFAULT CURRENT_TIMESTAMP," +
		"`last` DATETIME DEFAULT CURRENT_TIMESTAMP," +
		"`name` VARCHAR(16) NOT NULL," +
		"`account` VARCHAR(32) NOT NULL," +
		"`password` CHAR(128) NOT NULL," +
		"`order` INT UNSIGNED DEFAULT 0," +
		"`income` BIGINT UNSIGNED DEFAULT 0);"
	_, err := utils.Mysql.Exec(sql)
	if err != nil {
		return err
	}
	return nil
}

func (row *ManagerTable) Insert() error {
	sql := "INSERT INTO `manager`(`id`,`name`,`account`,`password`) VALUES(?,?,?,?);"
	pre, err := utils.Mysql.Prepare(sql)
	if err != nil {
		return err
	}
	_, err = pre.Exec(row.Id, row.Name, row.Account, row.Password)
	if err != nil {
		return err
	}
	return nil
}

func (row *ManagerTable) Delete() error {
	pre, err := utils.Mysql.Prepare("DELETE FROM `manager` WHERE `id`=?;")
	if err != nil {
		return err
	}
	_, err = pre.Exec(row.Id)
	if err != nil {
		return err
	}
	return nil
}

func (row *ManagerTable) Update(args utils.JsonType) error {
	keys, values := utils.UnZip(args)
	sql := utils.Format("UPDATE `manager` SET %s WHERE `id`='%s';", utils.SqlFields(keys), row.Id)
	pre, err := utils.Mysql.Prepare(sql)
	if err != nil {
		return err
	}
	_, err = pre.Exec(values...)
	if err != nil {
		return err
	}
	return nil
}

func (row *ManagerTable) UpdateSelf() error {
	sql := "UPDATE `manager` SET `super`=?,`last`=?,`name`=?,`account`=?,`password`=?,`order`=?,`income`=? WHERE `id`=?;"
	pre, err := utils.Mysql.Prepare(sql)
	if err != nil {
		return err
	}
	row.Last = utils.TimeNow()
	_, err = pre.Exec(row.Super, row.Last, row.Name, row.Account, row.Password, row.Order, row.Income, row.Id)
	if err != nil {
		return err
	}
	return nil
}

func (row *ManagerTable) Get() error {
	sql := "SELECT `super`,`first`,`last`,`name`,`account`,`password`,`order`,`income` FROM `manager` WHERE `id`=?;"
	pre, err := utils.Mysql.Prepare(sql)
	if err != nil {
		return err
	}
	err = pre.QueryRow(row.Id).Scan(
		&row.Super, &row.First, &row.Last, &row.Name,
		&row.Account, &row.Password, &row.Order, &row.Income,
	)
	if err != nil {
		return err
	}
	return nil
}

func (row *ManagerTable) GetByAcc() error {
	sql := "SELECT `id`,`super`,`first`,`last`,`name`,`password`,`order`,`income` FROM `manager` WHERE `account`=? LIMIT 1;"
	pre, err := utils.Mysql.Prepare(sql)
	if err != nil {
		return err
	}
	err = pre.QueryRow(row.Account).Scan(
		&row.Id, &row.Super, &row.First, &row.Last,
		&row.Name, &row.Password, &row.Order, &row.Income,
	)
	if err != nil {
		return err
	}
	return nil
}

func QueryManager(name string) ([]utils.JsonType, error) {
	like := utils.Format("%%%s%%", name)
	sql := "SELECT `id`,`name` FROM `manager` WHERE `super`=0 AND `name` LIKE ? LIMIT 10;"
	pre, err := utils.Mysql.Prepare(sql)
	if err != nil {
		return nil, err
	}
	rows, err := pre.Query(like)
	if err != nil {
		return nil, err
	}
	res := make([]utils.JsonType, 0)
	for rows.Next() {
		tid, tna := "", ""
		_ = rows.Scan(&tid, &tna)
		res = append(res, utils.JsonType{"id": tid, "name": tna})
	}
	_ = rows.Close()
	return res, nil
}
