package main

import (
	"embed"
	"flag"
	"log"
	"os"

	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	"github.com/wailsapp/wails/v2/pkg/options/mac"
	"github.com/wailsapp/wails/v2/pkg/options/windows"

	"codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app"
)

//go:embed all:frontend/dist
var assets embed.FS

func main() {
	// Parse command-line flags
	mode := flag.String("mode", "", "Application mode: dev or production (default: production)")
	flag.Parse()

	// Validate mode (empty means use default which is production)
	if *mode != "" && *mode != "dev" && *mode != "development" && *mode != "production" {
		log.Fatalf("Invalid mode: %s. Must be 'dev' or 'production'", *mode)
	}

	// Set environment variable for the application to use
	os.Setenv("MAPLEFILE_MODE", *mode)

	log.Printf("MapleFile starting in %s mode", *mode)

	// Initialize application with Wire DI
	application, err := app.InitializeApplication()
	if err != nil {
		log.Fatalf("Failed to initialize application: %v", err)
	}

	// Create Wails application with options
	err = wails.Run(&options.App{
		Title:  "MapleFile",
		Width:  1440,
		Height: 900,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		BackgroundColour: &options.RGBA{R: 236, G: 240, B: 241, A: 1},
		OnStartup:        application.Startup,
		OnShutdown:       application.Shutdown,
		Bind: []interface{}{
			application,
		},
		// Disable browser-style right-click context menu to prevent
		// "Refresh" from crashing the app
		EnableDefaultContextMenu: false,
		// Platform-specific options
		Mac: &mac.Options{
			// Use default title bar for standard macOS window behavior
			// (draggable title bar, proper traffic light buttons)
			TitleBar:             mac.TitleBarDefault(),
			WebviewIsTransparent: false,
			WindowIsTranslucent:  false,
		},
		Windows: &windows.Options{
			WebviewIsTransparent: false,
			WindowIsTranslucent:  false,
		},
	})

	if err != nil {
		log.Fatalf("Failed to run application: %v", err)
	}
}
