// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tests

import (
	"database/sql"
	"fmt"
	"testing"
	"time"

	"code.forgejo.org/xorm/xorm"
	"code.forgejo.org/xorm/xorm/internal/utils"
	"code.forgejo.org/xorm/xorm/names"
	"code.forgejo.org/xorm/xorm/schemas"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestTransaction(t *testing.T) {
	assert.NoError(t, PrepareEngine())
	assertSync(t, new(Userinfo))

	counter := func(t *testing.T) {
		_, err := testEngine.Count(&Userinfo{})
		assert.NoError(t, err)
	}

	counter(t)
	// defer counter()

	session := testEngine.NewSession()
	defer session.Close()

	err := session.Begin()
	assert.NoError(t, err)

	user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
	_, err = session.Insert(&user1)
	assert.NoError(t, err)

	user2 := Userinfo{Username: "yyy"}
	_, err = session.Where("`id` = ?", 0).Update(&user2)
	assert.NoError(t, err)

	_, err = session.Delete(&user2)
	assert.NoError(t, err)

	err = session.Commit()
	assert.NoError(t, err)
}

func TestCombineTransaction(t *testing.T) {
	assert.NoError(t, PrepareEngine())
	assertSync(t, new(Userinfo))

	counter := func() {
		total, err := testEngine.Count(&Userinfo{})
		assert.NoError(t, err)
		fmt.Printf("----now total %v records\n", total)
	}

	counter()
	// defer counter()
	session := testEngine.NewSession()
	defer session.Close()

	err := session.Begin()
	assert.NoError(t, err)

	user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
	_, err = session.Insert(&user1)
	assert.NoError(t, err)

	user2 := Userinfo{Username: "zzz"}
	_, err = session.Where("`id` = ?", 0).Update(&user2)
	assert.NoError(t, err)

	_, err = session.Exec("delete from "+testEngine.Quote(testEngine.TableName("userinfo", true))+" where `username` = ?", user2.Username)
	assert.NoError(t, err)

	err = session.Commit()
	assert.NoError(t, err)
}

func TestCombineTransactionSameMapper(t *testing.T) {
	assert.NoError(t, PrepareEngine())

	oldMapper := testEngine.GetColumnMapper()
	testEngine.UnMapType(utils.ReflectValue(new(Userinfo)).Type())
	testEngine.SetMapper(names.SameMapper{})
	defer func() {
		testEngine.UnMapType(utils.ReflectValue(new(Userinfo)).Type())
		testEngine.SetMapper(oldMapper)
	}()

	assertSync(t, new(Userinfo))

	counter := func() {
		total, err := testEngine.Count(&Userinfo{})
		assert.NoError(t, err)
		fmt.Printf("----now total %v records\n", total)
	}

	counter()
	defer counter()

	session := testEngine.NewSession()
	defer session.Close()

	err := session.Begin()
	assert.NoError(t, err)

	user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
	_, err = session.Insert(&user1)
	assert.NoError(t, err)

	user2 := Userinfo{Username: "zzz"}
	_, err = session.Where("`id` = ?", 0).Update(&user2)
	assert.NoError(t, err)

	_, err = session.Exec("delete from  "+testEngine.Quote(testEngine.TableName("Userinfo", true))+" where `Username` = ?", user2.Username)
	assert.NoError(t, err)

	err = session.Commit()
	assert.NoError(t, err)
}

func TestMultipleTransaction(t *testing.T) {
	assert.NoError(t, PrepareEngine())

	type MultipleTransaction struct {
		Id   int64
		Name string
	}

	assertSync(t, new(MultipleTransaction))

	session := testEngine.NewSession()
	defer session.Close()

	err := session.Begin()
	assert.NoError(t, err)

	m1 := MultipleTransaction{Name: "xiaoxiao2"}
	_, err = session.Insert(&m1)
	assert.NoError(t, err)

	user2 := MultipleTransaction{Name: "zzz"}
	_, err = session.Where("`id` = ?", 0).Update(&user2)
	assert.NoError(t, err)

	err = session.Commit()
	assert.NoError(t, err)

	var ms []MultipleTransaction
	err = session.Find(&ms)
	assert.NoError(t, err)
	assert.EqualValues(t, 1, len(ms))

	err = session.Begin()
	assert.NoError(t, err)

	_, err = session.Where("`id`=?", m1.Id).Delete(new(MultipleTransaction))
	assert.NoError(t, err)

	err = session.Commit()
	assert.NoError(t, err)

	ms = make([]MultipleTransaction, 0)
	err = session.Find(&ms)
	assert.NoError(t, err)
	assert.EqualValues(t, 0, len(ms))

	err = session.Begin()
	assert.NoError(t, err)

	_, err = session.Insert(&MultipleTransaction{
		Name: "ssss",
	})
	assert.NoError(t, err)

	err = session.Rollback()
	assert.NoError(t, err)

	ms = make([]MultipleTransaction, 0)
	err = session.Find(&ms)
	assert.NoError(t, err)
	assert.EqualValues(t, 0, len(ms))
}

func TestInsertMulti2InterfaceTransaction(t *testing.T) {
	type Multi2InterfaceTransaction struct {
		ID         uint64 `xorm:"id pk autoincr"`
		Name       string
		Alias      string
		CreateTime time.Time `xorm:"created"`
		UpdateTime time.Time `xorm:"updated"`
	}
	assert.NoError(t, PrepareEngine())
	assertSync(t, new(Multi2InterfaceTransaction))
	session := testEngine.NewSession()
	defer session.Close()
	err := session.Begin()
	assert.NoError(t, err)

	users := []any{
		&Multi2InterfaceTransaction{Name: "a", Alias: "A"},
		&Multi2InterfaceTransaction{Name: "b", Alias: "B"},
		&Multi2InterfaceTransaction{Name: "c", Alias: "C"},
		&Multi2InterfaceTransaction{Name: "d", Alias: "D"},
	}
	cnt, err := session.Insert(&users)

	assert.NoError(t, err)
	assert.EqualValues(t, len(users), cnt)

	assert.NotPanics(t, func() {
		err = session.Commit()
		assert.NoError(t, err)
	})
}

func TestTransactionIsolationLevels(t *testing.T) {
	if testEngine.Dialect().URI().DBType == schemas.SQLITE {
		t.Skip("Does not support isolation levels")
	}

	if testEngine.Dialect().URI().DBType == schemas.MYSQL {
		t.Skip("https://bugs.mysql.com/bug.php?id=53341")
	}

	getTransactionLevel := func(t *testing.T, sess *xorm.Session) string {
		t.Helper()
		var transactionLevel string
		switch testEngine.Dialect().URI().DBType {
		case schemas.POSTGRES:
			has, err := sess.SQL("SHOW TRANSACTION ISOLATION LEVEL").Get(&transactionLevel)
			assert.True(t, has)
			require.NoError(t, err)
		}
		return transactionLevel
	}

	expectedLevels := map[schemas.DBType]map[sql.IsolationLevel]string{
		schemas.POSTGRES: {
			sql.LevelDefault:         "read committed",
			sql.LevelReadUncommitted: "read uncommitted",
			sql.LevelRepeatableRead:  "repeatable read",
			sql.LevelReadCommitted:   "read committed",
			sql.LevelSerializable:    "serializable",
		},
	}[testEngine.Dialect().URI().DBType]

	t.Run("Default", func(t *testing.T) {
		session := testEngine.NewSession()
		defer session.Close()
		require.NoError(t, session.Begin())

		transactionLevel := getTransactionLevel(t, session)

		assert.Equal(t, expectedLevels[sql.LevelDefault], transactionLevel)
	})

	t.Run("Read Uncommitted", func(t *testing.T) {
		session := testEngine.NewSession()
		defer session.Close()
		require.NoError(t, session.BeginOpts(&sql.TxOptions{
			Isolation: sql.LevelReadUncommitted,
		}))

		transactionLevel := getTransactionLevel(t, session)
		assert.Equal(t, expectedLevels[sql.LevelReadUncommitted], transactionLevel)
	})

	t.Run("Read Committed", func(t *testing.T) {
		session := testEngine.NewSession()
		defer session.Close()
		require.NoError(t, session.BeginOpts(&sql.TxOptions{
			Isolation: sql.LevelReadCommitted,
		}))

		transactionLevel := getTransactionLevel(t, session)
		assert.Equal(t, expectedLevels[sql.LevelReadCommitted], transactionLevel)
	})

	t.Run("Repeatable Read", func(t *testing.T) {
		session := testEngine.NewSession()
		defer session.Close()
		require.NoError(t, session.BeginOpts(&sql.TxOptions{
			Isolation: sql.LevelRepeatableRead,
		}))

		transactionLevel := getTransactionLevel(t, session)
		assert.Equal(t, expectedLevels[sql.LevelRepeatableRead], transactionLevel)
	})

	t.Run("Serializable", func(t *testing.T) {
		session := testEngine.NewSession()
		defer session.Close()
		require.NoError(t, session.BeginOpts(&sql.TxOptions{
			Isolation: sql.LevelSerializable,
		}))

		transactionLevel := getTransactionLevel(t, session)
		assert.Equal(t, expectedLevels[sql.LevelSerializable], transactionLevel)
	})
}
