package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"time"

	"go.uber.org/zap"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/app"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/initialize"
)

var (
	commit    string = os.Getenv("COMMIT")
	buildTime string = os.Getenv("BUILD_TIME")
	version   string = os.Getenv("VERSION")
)

// $env:COMMIT=$(git rev-parse --short HEAD); $env:BUILD_TIME=$(Get-Date -UFormat "%Y-%m-%dT%H:%M:%SZ"); $env:VERSION=$(git describe --tags); go run cmd/app/main.go

func main() {
	logger, err := zap.NewProduction(zap.AddStacktrace(zap.DPanicLevel))
	if err != nil {
		log.Fatalf("failed to init zap logger: %v", err)
	}

	ptime, err := time.Parse(time.RFC3339, buildTime)
	if err != nil {
		logger.Error("Error parsing build time:", zap.Error(err))
		ptime = time.Now()
	}

	config, err := initialize.Configuration(".env.test")
	if err != nil {
		logger.Fatal("failed to init config: %v", zap.Error(err))
	}

	configJSON, err := json.MarshalIndent(config, "", "\t")
	if err != nil {
		logger.Error("failed to translate config struct to json for print inromation: %v", zap.Error(err))
	}

	fmt.Println("env configuration: \n", string(configJSON))

	if err := app.Run(config, logger, app.Build{
		Commit:    commit,
		Version:   version,
		BuildTime: ptime.Unix(),
	}); err != nil {
		logger.Fatal("failed to run app: %v", zap.Error(err))
	}
}
