package playground

import (
	"testing"

	"code.patial.tech/go/pgm"
	"code.patial.tech/go/pgm/playground/db"
	"code.patial.tech/go/pgm/playground/db/user"
)

func TestInsertQuery(t *testing.T) {
	got := db.User.Insert().
		Set(user.Email, "aa@aa.com").
		Set(user.Phone, 8889991234).
		Set(user.FirstName, "fname").
		Set(user.LastName, "lname").
		Returning(user.ID).
		String()

	expected := "INSERT INTO users(email, phone, first_name, last_name) VALUES($1, $2, $3, $4) RETURNING users.id"
	if got != expected {
		t.Errorf("\nexpected: %q\ngot: %q", expected, got)
	}
}

func TestInsertQuery2(t *testing.T) {
	got := db.User.Insert().
		Set(user.Email, "aa@aa.com").
		Set(user.Phone, 8889991234).
		OnConflict(user.ID).
		DoNothing().
		String()
	expected := "INSERT INTO users(email, phone) VALUES($1, $2) ON CONFLICT (id) DO NOTHING"
	if got != expected {
		t.Errorf("\nexpected: %q\ngot: %q", expected, got)
	}
}

// BenchmarkInsertQuery-12    	 2517757	       459.6 ns/op	    1032 B/op	      13 allocs/op
func BenchmarkInsertQuery(b *testing.B) {
	for b.Loop() {
		_ = db.User.Insert().
			Set(user.Email, "aa@aa.com").
			Set(user.Phone, 8889991234).
			Set(user.FirstName, "fname").
			Set(user.LastName, "lname").
			Returning(user.ID).
			String()

	}
}

// BenchmarkInsertSetMap-12    	 1812555	       644.1 ns/op	    1368 B/op	      15 allocs/op
func BenchmarkInsertSetMap(b *testing.B) {
	for b.Loop() {
		_ = db.User.Insert().
			SetMap(map[pgm.Field]any{
				user.Email:     "aa@aa.com",
				user.Phone:     8889991234,
				user.FirstName: "fname",
				user.LastName:  "lname",
			}).
			Returning(user.ID).
			String()

	}
}
