package storage

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/ClickHouse/clickhouse-go/v2"
	"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)

var Connection driver.Conn

func Connect() error {
	conn, err := connect()
	if err != nil {
		return err
	}
	Connection = *conn
	return nil
}

func Close() {
	Connection.Close()
}

func connect() (*driver.Conn, error) {
	var (
		ctx       = context.Background()
		conn, err = clickhouse.Open(&clickhouse.Options{
			Addr: []string{"clickhouse:9000"},
			Auth: clickhouse.Auth{
				Database: "monitoring",
				Username: "default",
				Password: os.Getenv("CLICKHOUSE_PASSWORD"),
			},
			TLS: nil,
		})
	)

	if err != nil {
		return nil, err
	}

	if err := conn.Ping(ctx); err != nil {
		if exception, ok := err.(*clickhouse.Exception); ok {
			fmt.Printf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
		}
		return nil, err
	}
	return &conn, nil
}

func Migrate() error {
	err := Connection.Exec(
		context.TODO(),
		`CREATE TABLE IF NOT EXISTS endpoints (
			timestamp        DateTime,
			service          LowCardinality(String),
			environment      LowCardinality(String),
			endpoint         LowCardinality(String),
			status_code      UInt16,
			response_time_ms UInt32,
			method           LowCardinality(String)
		)
		ENGINE = MergeTree
		PARTITION BY toYYYYMM(timestamp)
		ORDER BY (service, environment, endpoint, method, timestamp);`,
	)
	if err != nil {
		log.Fatal(err)
		return err
	}

	err = Connection.Exec(
		context.TODO(),
		`CREATE TABLE IF NOT EXISTS tasks (
			timestamp         DateTime,
			service           LowCardinality(String),
			environment       LowCardinality(String),
			queue             LowCardinality(String),
			success           Bool,
			execution_time_ms UInt32
		)
		ENGINE = MergeTree
		PARTITION BY toYYYYMM(timestamp)
		ORDER BY (service, environment, queue, timestamp);`,
	)
	if err != nil {
		log.Fatal(err)
		return err
	}
	return nil
}
