package main

import (
	"context"
	"embed"
	"log"

	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
)

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

func main() {
	logger, err := InitLogger()
	if err != nil {
		log.Fatalln("logger init:", err)
	}
	defer logger.Close()
	logger.Info("starting matrix_client_pc", "version", "0.1.0")

	// NOTE: WebView2 strips --remote-debugging-port from
	// WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS for security, so CDP attach
	// to the production WebView2 is not feasible. Frontend automation
	// uses `wails dev -browser` (Chrome with CDP) instead. The E2E HTTP
	// server on :8767 remains the canonical driver for headless tests.

	ms := NewMatrixService()

	err = wails.Run(&options.App{
		Title:  "matrix_client_pc",
		Width:  1280,
		Height: 800,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		BackgroundColour: &options.RGBA{R: 26, G: 27, B: 30, A: 1},
		OnStartup: func(ctx context.Context) {
			ms.SetContext(ctx)
			logger.Info("wails ctx ready")
			MaybeStartE2EServer(ms)
		},
		OnShutdown: func(ctx context.Context) {
			logger.Info("shutdown")
		},
		Bind: []interface{}{
			ms,
		},
	})
	if err != nil {
		logger.Error("wails error", "err", err)
		log.Fatalln("Wails error:", err)
	}
}
