package agent

import (
	"context"
	"time"

	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
)

type AgentInfo struct {
	Id            int
	Status        string
	AgentName     string
	AgentPassword string
	PayPassword   string
	AgentRemark   string
	AgentUid      string
	AgentPhone    string
	UpdateTime    time.Time
	CreateTime    time.Time
}

const AGENT_INFO = "agent_info"

func IsEixstByAgentName(ctx context.Context, agentName string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(AGENT_INFO).Filter("agent_name", agentName).ExistWithCtx(ctx)

	return exist
}

func IsExistByAgentUid(ctx context.Context, uid string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(AGENT_INFO).Filter("agent_uid", uid).ExistWithCtx(ctx)

	return exist
}

func IsEixstByAgentPhone(ctx context.Context, agentPhone string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(AGENT_INFO).Filter("agent_phone", agentPhone).ExistWithCtx(ctx)
	return exist
}

func InsertAgentInfo(ctx context.Context, agentInfo AgentInfo) bool {
	o := orm.NewOrm()
	_, err := o.InsertWithCtx(ctx, &agentInfo)
	if err != nil {
		logs.Error("insert agent info fail: ", err)
		return false
	}

	return true
}

func GetAgentInfoByAgentUid(ctx context.Context, agentUid string) AgentInfo {
	o := orm.NewOrm()
	var agentInfo AgentInfo
	_, err := o.QueryTable(AGENT_INFO).Filter("agent_uid", agentUid).Limit(1).AllWithCtx(ctx, &agentInfo)

	if err != nil {
		logs.Error("get agent info by agentUid fail: ", err)
	}

	return agentInfo
}

func GetAgentInfoLenByMap(ctx context.Context, params map[string]string) int {
	o := orm.NewOrm()
	qs := o.QueryTable(AGENT_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}
	cnt, err := qs.Limit(-1).CountWithCtx(ctx)
	if err != nil {
		logs.Error("get agentinfo len by map fail: ", err)
	}

	return int(cnt)
}

func GetAgentInfoByMap(ctx context.Context, params map[string]string, displayCount, offset int) []AgentInfo {
	o := orm.NewOrm()
	var agentInfoList []AgentInfo

	qs := o.QueryTable(AGENT_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}

	_, err := qs.Limit(displayCount, offset).OrderBy("-update_time").AllWithCtx(ctx, &agentInfoList)

	if err != nil {
		logs.Error("get agentInfo by map fail: ", err)
	}

	return agentInfoList
}

func GetAllAgentByMap(ctx context.Context, parmas map[string]string) []AgentInfo {
	o := orm.NewOrm()
	var agentList []AgentInfo

	qs := o.QueryTable(AGENT_INFO)
	for k, v := range parmas {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}

	_, err := qs.Limit(-1).AllWithCtx(ctx, &agentList)
	if err != nil {
		logs.Error("get all agent by map fail: ", err)
	}

	return agentList
}

func UpdateAgentInfo(ctx context.Context, agentInfo AgentInfo) bool {
	o := orm.NewOrm()
	_, err := o.UpdateWithCtx(ctx, &agentInfo)

	if err != nil {
		logs.Error("update agentinfo fail: ", err)
		return false
	}

	return true
}

func DeleteAgentByAgentUid(ctx context.Context, agentUid string) bool {
	o := orm.NewOrm()
	_, err := o.QueryTable(AGENT_INFO).Filter("agent_uid", agentUid).DeleteWithCtx(ctx)
	if err != nil {
		logs.Error("delete agent by agent uid fail: ", err)
		return false
	}
	return true
}
