//go:build integration
// +build integration

package integration_test

import (
	"fmt"
	"testing"

	"github.com/everything-on-mysql/cassandra-on-mysql/pkg/cql"
)

func TestUnsupportedFeaturesInvalid(t *testing.T) {
	session := newSession(t, sessionOptions{disableHostLookup: true})

	keyspace := uniqueKeyspace()
	if err := session.Query(createKeyspaceCQL(keyspace)).Exec(); err != nil {
		t.Fatalf("create keyspace failed: %v", err)
	}
	t.Cleanup(func() {
		_ = session.Query(dropKeyspaceCQL(keyspace)).Exec()
	})

	table := "unsupported"
	if err := session.Query(createTableCQL(keyspace, table)).Exec(); err != nil {
		t.Fatalf("create table failed: %v", err)
	}

	queries := []string{
		fmt.Sprintf("SELECT * FROM %s.%s GROUP BY id HAVING COUNT(*) > 0 ALLOW FILTERING", keyspace, table),
		fmt.Sprintf("SELECT WRITETIME(name) FROM %s.%s WHERE id = 1", keyspace, table),
		fmt.Sprintf("SELECT * FROM %s.%s WHERE id = NULL", keyspace, table),
		fmt.Sprintf("INSERT INTO %s.%s (id, name, idx) VALUES (1, 'a', 1) USING TTL 60", keyspace, table),
		fmt.Sprintf("UPDATE %s.%s USING TTL 60 SET name = 'b' WHERE id = 1", keyspace, table),
		fmt.Sprintf("UPDATE %s.%s SET name = 'b' WHERE id = NULL", keyspace, table),
		fmt.Sprintf("UPDATE %s.%s SET id = 2 WHERE id = 1", keyspace, table),
		fmt.Sprintf("DELETE FROM %s.%s WHERE id = NULL", keyspace, table),
		fmt.Sprintf("BEGIN BATCH UPDATE %s.%s SET name = 'b' WHERE id = 1 IF EXISTS APPLY BATCH", keyspace, table),
	}

	for _, query := range queries {
		t.Run(query, func(t *testing.T) {
			err := session.Query(query).Exec()
			requireErrorCode(t, err, cql.ErrorCodeInvalid)
		})
	}
}
