package test

import (
	"fmt"
	"testing"

	log2 "github.com/pingcap-inc/clinic/clinic_data_service/pkg/log"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/thirdapi/feishu"

	"go.uber.org/zap"
)

func TestCreateTable(t *testing.T) {

	// Initialize Feishu client
	client := feishu.NewClient("cli_a8c5690eb1b6500c", "w7Mx8u4jNHn07fDiTL0aRmBmN4LMH1jy")

	// Document parameters
	documentID := "ZzD8weVxni8wcskmEhIcXa3an6c"
	// parentBlockID := "ZzD8weVxni8wcskmEhIcXa3an6c"

	parentBlockID := "doxcnFDKXxBZlRYgblOVsxrMMGc"
	// parentBlockID := "doxcnSbIeq4fnPvQjWbLpX8rtSf"

	// Data rows (field order: link, link, text, text, number, single-select, single-select, text)
	data := [][]feishu.CellValue{
		{
			feishu.NewLinkCell("35aeb44279b17975b7fba8901526b1d556cef4c69f4caca27a7b6c6f75674ae4", "https://clinic.pingcap.com/portal/dashboard/cloud/ngm.html?provider=aws&region=us-east-1&orgName=Buffet%27s%20Org&orgId=1372813089209061633&projectId=1372813089454544954&clusterId=1379661944646416027&clusterName=prod-us-saas-region01-tidb-v75&deployType=dedicated#/slow_query?digest=35aeb44279b17975b7fba8901526b1d556cef4c69f4caca27a7b6c6f75674ae4&from=1749686400&row=0&to=1749772797"),
			feishu.TextCell("saas01"),
			feishu.TextCell(""),
			feishu.NewLinkCell("SELECT useridx, sum(if(direct=1 and member_is_agent=1, 1, 0)) as direct_agents, sum(if(direct=1 and member_is_agent=0, 1, 0)) as direct_accounts, sum(if(direct=0 and member_is_agent=1, 1, 0)) as other_agents, sum(if(direct=0 and member_is_agent=0, 1, 0)) as other_accounts FROM `dh_promote_myteam_detail` WHERE useridx = ? AND site_code = ? [arguments: (36193237, \"5252\")];", "https://clinic.pingcap.com/portal/dashboard/cloud/ngm.html?provider=aws&region=us-east-1&orgName=Buffet%27s%20Org&orgId=1372813089209061633&projectId=1372813089454544954&clusterId=1379661944646416027&clusterName=prod-us-saas-region01-tidb-v75&deployType=dedicated#/slow_query/detail?digest=35aeb44279b17975b7fba8901526b1d556cef4c69f4caca27a7b6c6f75674ae4&connection_id=2701247320&timestamp=1749710771.967943"),
			feishu.TextCell("1.19s/2.78s"),
			feishu.TextCell("RU111"),
			feishu.TextCell("Memory 01"),
			feishu.NumberCell(11047),
			feishu.TextCell("MaxCPUTimeIn5MinWindow222"),
			feishu.TextCell("Max5MinWindowTime(UTC)2222"),
			feishu.NewLinkCell("MaxInstance（TopSql link)", ""),
			feishu.TextCell("TotalCPUTimeInDay22"),
			feishu.TextCell("PatternSuggestion tddd"),
		},
	}

	// Use new BiTable creation workflow
	fmt.Println("Creating BiTable...")
	appToken, err := client.CreateBiTableAndInsertData(documentID, parentBlockID, data, feishu.SlowQueryTableFieldConfigs)
	if err != nil {
		log2.PlainError("Failed to create BiTable", zap.Error(err))
		t.Fatalf("Failed to create BiTable: %v", err)
	}
	fmt.Printf("Successfully created BiTable, App Token: %s\n", appToken)
}
