package parquet

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/observability/cmd/diagnosis-compute/pkg/types"
)

func TestS3ObjectKey(t *testing.T) {
	info := types.ClusterInfo{
		Env:         "prod",
		TenantID:    "org1",
		ClusterID:   "clusterA",
		ClusterType: types.ClusterTypeDedicated,
	}
	hourBegin := time.Date(2026, 6, 18, 14, 0, 0, 0, time.UTC).Unix()
	key := S3ObjectKey(info, hourBegin, "nginx", 1)
	assert.Equal(t, "deltalake/global/logs/env=prod/deployType=dedicated/date=2026-06-18/component=nginx/14_clusterA_01.parquet", key)
}

func TestLogToRow(t *testing.T) {
	raw := "2023-11-20T07:05:56.858176156Z;ip-172-30-8-211.ec2.internal;tidb1379661944646413139;db-tidb-0;tidb [2023/11/20 07:05:56.858 +00:00] [INFO] [cpuprofile.go:113] [\"test\"]"
	item := &types.Log{
		RawLine:   raw,
		Timestamp: 1700463956,
		Level:     "INFO",
		Position:  "cpuprofile.go:113",
		Message:   "[\"test\"]",
		Instance:  "db-tidb-0",
	}
	item.SetEventTime(time.Date(2023, 11, 20, 7, 5, 56, 858176156, time.UTC))
	row := LogToRow(item, "cluster-1", "org-1")
	assert.Equal(t, "ip-172-30-8-211.ec2.internal", row.Host)
	assert.Equal(t, "db-tidb-0", row.Pod)
	assert.Equal(t, "db-tidb-0", row.Instance)
	assert.Equal(t, "cluster-1", row.Cluster)
	assert.Equal(t, "org-1", row.TenantID)
	assert.Equal(t, int64(1700463956), row.Timestamp)
	assert.Equal(t, "2023-11-20T07:05:56.858176156Z", row.EventTime)
}

func TestLogToRowFallsBackToUnixSeconds(t *testing.T) {
	item := &types.Log{
		Timestamp: 1700463956,
		Level:     "INFO",
		Message:   "x",
		Instance:  "db-tidb-0",
	}
	row := LogToRow(item, "cluster-1", "org-1")
	assert.Equal(t, "2023-11-20T07:05:56Z", row.EventTime)
}
