package main

import (
	"fmt"
	"miasma-installer/tui"
	"os"

	tea "github.com/charmbracelet/bubbletea"
)

var Version = "dev"

func main() {
	if os.Geteuid() != 0 {
		fmt.Println("Error: This installer must be run as root")
		fmt.Println("Please run: sudo miasma-installer")
		os.Exit(1)
	}

	if _, err := os.Stat("/sys/firmware/efi"); os.IsNotExist(err) {
		fmt.Println("Error: UEFI boot mode not detected")
		fmt.Println("Miasma OS requires UEFI boot mode")
		os.Exit(1)
	}

	p := tea.NewProgram(tui.NewModel(), tea.WithAltScreen())

	finalModel, err := p.Run()
	if err != nil {
		fmt.Printf("\nAn error occurred during installation: %v\n", err)
		os.Exit(1)
	}

	if root, ok := finalModel.(tui.RootModel); ok {
		if root.State == tui.StateFinished {
			fmt.Println("\n✓ Miasma OS Installation Complete!")
			fmt.Println("\nThe system will reboot in 5 seconds...")
			fmt.Println("Press Ctrl+C to cancel reboot")
			
			fmt.Print("\nUnmounting filesystems...")
			os.Exit(0)
		}
	}
}
