package main

import (
	"epochsilicon/pkg/debug"
	"epochsilicon/pkg/service"
	"epochsilicon/pkg/ui"
	"epochsilicon/pkg/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"strings"
)

const appVersion = "1.0.0"

func main() {
	PEApp := app.NewWithID("com.burkey.epochsilicon")
	PEWindow := PEApp.NewWindow("EpochSilicon v" + appVersion)
	PEWindow.Resize(fyne.NewSize(650, 500))
	PEWindow.SetFixedSize(true)

	go func() {
		prefs, _ := utils.LoadPrefs()
		updateInfo, updateAvailable, err := utils.CheckForUpdateWithAssets(appVersion)
		if err != nil {
			debug.Printf("Failed to check for updates: %v", err)
			return
		}

		if !updateAvailable {
			debug.Printf("No updates available")
			return
		}

		latestVersion := strings.TrimPrefix(updateInfo.TagName, "v")
		debug.Printf("Update available: current=%s, latest=%s", appVersion, latestVersion)

		// Skip if user has suppressed this version
		if prefs.SuppressedUpdateVersion == latestVersion {
			debug.Printf("Update suppressed by user: %s", latestVersion)
			return
		}

		// Show enhanced update dialog
		ui.ShowUpdateDialog(updateInfo, appVersion, PEWindow)
	}()

	content := ui.CreateUI(PEWindow)
	PEWindow.SetContent(content)

	// Set up cleanup when window closes
	PEWindow.SetCloseIntercept(func() {
		debug.Println("Application closing, cleaning up RosettaX87 service...")
		service.CleanupService()
		PEApp.Quit()
	})

	PEWindow.ShowAndRun()
}
