package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"git.linuxforward.com/byop/byop-engine/app"
	"git.linuxforward.com/byop/byop-engine/config"
)

// Version information
var (
	Version   = "0.1.0"
	BuildTime = "unknown"
	GitCommit = "unknown"
)

func printVersion() {
	fmt.Printf("BYOP Engine v%s\n", Version)
	fmt.Printf("Build time: %s\n", BuildTime)
	fmt.Printf("Git commit: %s\n", GitCommit)
}

func printHelp() {
	fmt.Printf("BYOP Engine - Bring Your Own Platform engine\n\n")
	fmt.Printf("Usage: %s [options]\n\n", os.Args[0])
	fmt.Println("Options:")
	flag.PrintDefaults()
}

func main() {
	// 1. Parse command line flags
	configPath := flag.String("config", "./config.yaml", "Path to configuration file")
	showVersion := flag.Bool("version", false, "Display version information and exit")
	showHelp := flag.Bool("help", false, "Display help information and exit")
	flag.Parse()

	// Handle version and help flags
	if *showVersion {
		printVersion()
		os.Exit(0)
	}

	if *showHelp {
		printHelp()
		os.Exit(0)
	}

	// 2. Parse configuration
	cfg, err := config.Load(*configPath)
	if err != nil {
		log.Fatalf("Failed to load configuration: %v", err)
	}

	// 3. Initialize application
	app, err := app.NewApp(cfg)
	if err != nil {
		log.Fatalf("Failed to initialize application: %v", err)
	}

	// 4. Start application
	if err := app.Run(); err != nil {
		log.Fatalf("Failed to start application: %v", err)
	}
}
