package tablestoredb

import (
	"fmt"
	"testing"

	"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)

func TestTableStoreInsert(t *testing.T) {
	ots := &TableStore{
		EndPoint:     "https://pai-test.cn-beijing.ots.aliyuncs.com",
		InstanceName: "pai-test",
		RoleArn:      "acs:ram::1730760139076263:role/otsadmin",
	}

	err := ots.Init()
	if err != nil {
		t.Error(err)
	}

	putRowRequest := new(tablestore.PutRowRequest)
	putRowChange := new(tablestore.PutRowChange)
	putRowChange.TableName = "user_expose_log"
	putPk := new(tablestore.PrimaryKey)
	putPk.AddPrimaryKeyColumn("did", "123456")

	putRowChange.PrimaryKey = putPk
	putRowChange.AddColumn("itemid", "itemid_123")
	putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
	putRowRequest.PutRowChange = putRowChange
	_, err = ots.Client.PutRow(putRowRequest)

	if err != nil {
		fmt.Println("putrow failed with error:", err)
		t.Error(err)
	} else {
		fmt.Println("putrow finished")
	}

	getRowRequest := new(tablestore.GetRowRequest)
	criteria := new(tablestore.SingleRowQueryCriteria)
	putPk = new(tablestore.PrimaryKey)
	putPk.AddPrimaryKeyColumn("did", "123456")

	criteria.PrimaryKey = putPk
	getRowRequest.SingleRowQueryCriteria = criteria
	getRowRequest.SingleRowQueryCriteria.TableName = "user_expose_log"
	getRowRequest.SingleRowQueryCriteria.MaxVersion = 1
	getResp, err := ots.Client.GetRow(getRowRequest)
	if err != nil {
		fmt.Println("getrow failed with error:", err)
	} else {
		fmt.Println("get row col0 result is ", getResp.Columns[0].ColumnName, getResp.Columns[0].Value)
	}
}
func TestTableStoreGet(t *testing.T) {
	ots := &TableStore{
		EndPoint:     "https://pai-test.cn-beijing.ots.aliyuncs.com",
		InstanceName: "pai-test",
		RoleArn:      "acs:ram::1730760139076263:role/otsadmin",
	}
	err := ots.Init()
	if err != nil {
		t.Error(err)
	}

	getRowRequest := new(tablestore.GetRowRequest)
	criteria := new(tablestore.SingleRowQueryCriteria)
	putPk := new(tablestore.PrimaryKey)
	putPk.AddPrimaryKeyColumn("topic", "topic_1")

	criteria.PrimaryKey = putPk

	getRowRequest.SingleRowQueryCriteria = criteria
	getRowRequest.SingleRowQueryCriteria.TableName = "user_topic"
	getRowRequest.SingleRowQueryCriteria.MaxVersion = 10

	getResp, err := ots.Client.GetRow(getRowRequest)

	fmt.Println(getResp, err, len(getResp.Columns))

	for _, col := range getResp.Columns {
		fmt.Println(col)
	}
}
func TestPutRow(t *testing.T) {
	ots := &TableStore{
		EndPoint:     "https://pai-test.cn-beijing.ots.aliyuncs.com",
		InstanceName: "pai-test",
		RoleArn:      "acs:ram::1730760139076263:role/otsadmin",
	}
	err := ots.Init()
	if err != nil {
		t.Error(err)
	}

	putRowRequest := new(tablestore.PutRowRequest)
	putRowChange := new(tablestore.PutRowChange)
	putRowChange.TableName = "topic_item"
	putPk := new(tablestore.PrimaryKey)
	// putPk.AddPrimaryKeyColumn("id", "")
	putPk.AddPrimaryKeyColumnWithAutoIncrement("id")
	//
	putRowChange.PrimaryKey = putPk
	putRowChange.AddColumn("topic", "topic_1")
	putRowChange.AddColumn("item_id", "123456")
	putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
	putRowRequest.PutRowChange = putRowChange
	resp, err1 := ots.Client.PutRow(putRowRequest)

	fmt.Println(resp, err1)
	if err1 != nil {
		fmt.Println("putrow failed with error:", err1)
	} else {
		fmt.Println("putrow finished")
	}
}

func TestUpdateRow(t *testing.T) {
	ots := &TableStore{
		EndPoint:     "https://pai-test.cn-beijing.ots.aliyuncs.com",
		InstanceName: "pai-test",
		RoleArn:      "acs:ram::1730760139076263:role/otsadmin",
	}
	err := ots.Init()
	if err != nil {
		t.Error(err)
	}

	putRowRequest := new(tablestore.UpdateRowRequest)
	putRowChange := new(tablestore.UpdateRowChange)
	putRowChange.TableName = "user_topic"
	putPk := new(tablestore.PrimaryKey)
	putPk.AddPrimaryKeyColumn("topic", "topic_1")

	putRowChange.PrimaryKey = putPk
	putRowChange.PutColumn("item_id", "1234")
	putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
	putRowRequest.UpdateRowChange = putRowChange
	resp, err1 := ots.Client.UpdateRow(putRowRequest)

	fmt.Println(resp, err1)
	if err1 != nil {
		fmt.Println("putrow failed with error:", err1)
	} else {
		fmt.Println("putrow finished")
	}
}
