package main

import (
	"flag"
	"fmt"
	"os"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

var (
	version = "dev"
	commit  = "unknown"
)

func main() {
	// Flags
	serverFlag := flag.String("server", "", "Server URL (e.g. https://chat.example.com)")
	tokenFlag := flag.String("token", "", "Session token (skips login)")
	versionFlag := flag.Bool("version", false, "Print version and exit")
	flag.Parse()

	if *versionFlag {
		fmt.Printf("dumpster-tui %s (%s)\n", version, commit)
		os.Exit(0)
	}

	// Load config
	cfg, err := LoadConfig()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
		os.Exit(1)
	}

	// Apply flags over config
	if *serverFlag != "" {
		cfg.ServerURL = *serverFlag
	}
	if *tokenFlag != "" {
		cfg.Token = *tokenFlag
	}

	// Create API client
	api := NewAPIClient(cfg.ServerURL)

	// Auth flow
	if cfg.Token == "" {
		token, err := RunAuth(api)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err)
			os.Exit(1)
		}
		cfg.Token = token

		// Save token for future sessions
		if err := SaveConfig(cfg); err != nil {
			fmt.Fprintf(os.Stderr, "Warning: could not save config: %v\n", err)
		}
	} else {
		api.SetToken(cfg.Token)
	}

	// Verify auth
	user, err := api.GetMe()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Session invalid or expired: %v\n", err)
		fmt.Fprintf(os.Stderr, "Try logging in again.\n")
		os.Exit(1)
	}
	fmt.Println(SuccessStyle.Render(fmt.Sprintf("  ✓ Logged in as %s", user.Username)))

	// Start TUI
	m := initialModel(api)
	m.user = *user

	p := tea.NewProgram(m,
		tea.WithAltScreen(),
		tea.WithMouseCellMotion(),
	)

	lipgloss.SetHasDarkBackground(true)

	if _, err := p.Run(); err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
}
