package worker

import (
	"Wine-Server/utils"
	"Wine-Server/utils/tables"
	"github.com/gorilla/websocket"
)

func query(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerQueryRes", utils.Fail("参数错误")))
		return
	}
	total, workers, err := tables.WorkerQueryForAdmin(manager.Id, param.Cond, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerQueryRes", utils.Fail("查询上酒工失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"adminWorkerQueryRes",
		utils.Success(utils.JsonType{"total": total, "list": workers}),
	))
}

func update(conn *websocket.Conn, data any) {
	var param updateParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerUpdateRes", utils.Fail("参数错误")))
		return
	}
	if param.Phone == "" {
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerUpdateRes", utils.Fail("联系电话不可为空")))
		return
	}
	worker := tables.WorkerTable{Id: param.Id}
	err = worker.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerUpdateRes", utils.Fail("更新失败 01")))
		return
	}
	if param.Password != "" {
		password, err := utils.Decrypt(param.Password)
		if err != nil {
			utils.Logger.Println(err)
			_ = conn.WriteJSON(utils.WsEvent("adminWorkerUpdateRes", utils.Fail("解密失败")))
			return
		}
		worker.Password = utils.HashPassword(password)
	}
	worker.Name, worker.Phone = param.Name, param.Phone
	err = worker.UpdateSelf()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerUpdateRes", utils.Fail("更新失败 02")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminWorkerUpdateRes", utils.Success(nil)))
	// TODO: inform worker login expired
}

func remove(conn *websocket.Conn, data any) {
	var ids []string
	err := utils.AnyTrans(data, &ids)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerDeleteRes", utils.Fail("参数错误")))
		return
	}
	err = tables.WorkersDelete(ids)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerDeleteRes", utils.Fail("删除失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminWorkerDeleteRes", utils.Success(nil)))
}

func add(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	var param addParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerAddRes", utils.Fail("参数错误")))
		return
	}
	if param.Name == "" || param.Phone == "" || param.Password == "" {
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerAddRes", utils.Fail("数据不可为空")))
		return
	}
	var password string
	password, err = utils.Decrypt(param.Password)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerAddRes", utils.Fail("解密失败")))
		return
	}
	id := utils.RandomString(16, utils.AlphaNumPatten)
	worker := &tables.WorkerTable{
		Id: id, Name: param.Name, Phone: param.Phone,
		Password: utils.HashPassword(password), Manager: manager.Id,
	}
	err = worker.Insert()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWorkerAddRes", utils.Fail("添加失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminWorkerAddRes", utils.Success(nil)))
}
