package model

import (
	"context"
	"fmt"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ AgentModel = (*customAgentModel)(nil)

type (
	// AgentModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAgentModel.
	AgentModel interface {
		agentModel
		// UpdateInTransaction 在事务中更新刚插入的记录，避免 UpdateWithVersion 中的 FindOne 缓存问题
		UpdateInTransaction(ctx context.Context, session sqlx.Session, data *Agent) error
	}

	customAgentModel struct {
		*defaultAgentModel
	}
)

// NewAgentModel returns a model for the database table.
func NewAgentModel(conn sqlx.SqlConn, c cache.CacheConf) AgentModel {
	return &customAgentModel{
		defaultAgentModel: newAgentModel(conn, c),
	}
}

// UpdateInTransaction 在事务中更新刚插入的记录，避免 UpdateWithVersion 中的 FindOne 缓存问题
// 注意：此方法假设记录刚插入，version 为 0，更新后 version 为 1
func (m *customAgentModel) UpdateInTransaction(ctx context.Context, session sqlx.Session, data *Agent) error {
	if session == nil {
		return errors.New("session is required for UpdateInTransaction")
	}

	// 直接执行 SQL UPDATE，避免 FindOne 的缓存问题
	// 字段顺序：user_id, level, region, mobile, wechat_id, team_leader_id, delete_time, del_state, version
	query := fmt.Sprintf("update %s set `user_id`=?, `level`=?, `region`=?, `mobile`=?, `wechat_id`=?, `team_leader_id`=?, `delete_time`=?, `del_state`=?, `version`=? where `id`=? and `version`=?", m.table)
	
	// 确保 version 从 0 增加到 1
	oldVersion := data.Version
	data.Version = oldVersion + 1
	
	result, err := session.ExecCtx(ctx, query, data.UserId, data.Level, data.Region, data.Mobile, data.WechatId, data.TeamLeaderId, data.DeleteTime, data.DelState, data.Version, data.Id, oldVersion)
	if err != nil {
		return errors.Wrapf(err, "UpdateInTransaction failed")
	}
	
	rowsAffected, err := result.RowsAffected()
	if err != nil {
		return errors.Wrapf(err, "UpdateInTransaction failed to get rows affected")
	}
	
	if rowsAffected == 0 {
		return errors.Wrapf(ErrNoRowsUpdate, "UpdateInTransaction: no rows updated, version may not match")
	}
	
	return nil
}
