package ui

import (
	"fmt"
	"strings"

	"github.com/charmbracelet/lipgloss"
)

// RenderConfigInit renders config initialization UI
func RenderConfigInit() string {
	title := "Drip Configuration Setup"
	box := boxStyle.Width(50)
	return "\n" + box.Render(titleStyle.Render(title)) + "\n"
}

// RenderConfigShow renders the config display
func RenderConfigShow(server, token string, tokenHidden bool, tlsEnabled bool, configPath string) string {
	lines := []string{
		KeyValue("Server", server),
	}

	if token != "" {
		if tokenHidden {
			if len(token) > 10 {
				displayToken := token[:3] + "***" + token[len(token)-3:]
				lines = append(lines, KeyValue("Token", Muted(displayToken+" (hidden)")))
			} else {
				lines = append(lines, KeyValue("Token", Muted(token[:3]+"*** (hidden)")))
			}
		} else {
			lines = append(lines, KeyValue("Token", token))
		}
	} else {
		lines = append(lines, KeyValue("Token", Muted("(not set)")))
	}

	tlsStatus := "enabled"
	if !tlsEnabled {
		tlsStatus = "disabled"
	}
	lines = append(lines, KeyValue("TLS", tlsStatus))
	lines = append(lines, KeyValue("Config", Muted(configPath)))

	return Info("Current Configuration", lines...)
}

// RenderConfigSaved renders config saved message
func RenderConfigSaved(configPath string) string {
	return SuccessBox(
		"Configuration Saved",
		Muted("Config saved to: ")+configPath,
		"",
		Muted("You can now use 'drip' without --server and --token flags"),
	)
}

// RenderConfigUpdated renders config updated message
func RenderConfigUpdated(updates []string) string {
	lines := make([]string, len(updates)+1)
	for i, update := range updates {
		lines[i] = Success(update)
	}
	lines[len(updates)] = ""
	lines = append(lines, Muted("Configuration has been updated"))
	return SuccessBox("Configuration Updated", lines...)
}

// RenderConfigDeleted renders config deleted message
func RenderConfigDeleted() string {
	return SuccessBox("Configuration Deleted", Muted("Configuration file has been removed"))
}

// RenderConfigValidation renders config validation results
func RenderConfigValidation(serverValid bool, serverMsg string, tokenSet bool, tokenMsg string, tlsEnabled bool) string {
	lines := []string{}

	if serverValid {
		lines = append(lines, Success(serverMsg))
	} else {
		lines = append(lines, Error(serverMsg))
	}

	if tokenSet {
		lines = append(lines, Success(tokenMsg))
	} else {
		lines = append(lines, Warning(tokenMsg))
	}

	if tlsEnabled {
		lines = append(lines, Success("TLS is enabled"))
	} else {
		lines = append(lines, Warning("TLS is disabled (not recommended for production)"))
	}

	lines = append(lines, "")
	lines = append(lines, Muted("Configuration validation complete"))

	if serverValid && tokenSet && tlsEnabled {
		return SuccessBox("Configuration Valid", lines...)
	}
	return WarningBox("Configuration Validation", lines...)
}

// RenderDaemonStarted renders daemon started message
func RenderDaemonStarted(tunnelType string, port int, pid int, logPath string, url string, forwardAddr string, serverAddr string) string {
	if forwardAddr == "" {
		forwardAddr = fmt.Sprintf("localhost:%d", port)
	}

	urlLine := Muted("(resolving...)")
	if url != "" {
		urlBadge := lipgloss.NewStyle().
			Background(successColor).
			Foreground(lipgloss.Color("#f8fafc")).
			Bold(true).
			Padding(0, 1).
			Render(url)
		urlLine = urlBadge
	}

	headline := successStyle.Render("✓ Tunnel Started in Background")

	lines := []string{
		KeyValue("Type", Highlight(tunnelType)),
		KeyValue("Port", fmt.Sprintf("%d", port)),
		KeyValue("PID", fmt.Sprintf("%d", pid)),
		KeyValue("Forward", forwardAddr),
	}
	if serverAddr != "" {
		lines = append(lines, KeyValue("Server", serverAddr))
	}

	lines = append(lines,
		"",
		Muted("Commands:"),
		Cyan("  drip list")+Muted("           Check tunnel status"),
		Cyan(fmt.Sprintf("  drip attach %s %d", tunnelType, port))+Muted("  View logs"),
		Cyan(fmt.Sprintf("  drip stop %s %d", tunnelType, port))+Muted("    Stop tunnel"),
		"",
		Muted("Logs: ")+mutedStyle.Render(logPath),
	)

	contentWidth := 0
	for _, line := range append([]string{headline}, lines...) {
		if w := lipgloss.Width(line); w > contentWidth {
			contentWidth = w
		}
	}
	if w := lipgloss.Width(urlLine); w > contentWidth {
		contentWidth = w
	}

	centeredURL := lipgloss.PlaceHorizontal(contentWidth, lipgloss.Center, urlLine)

	contentLines := make([]string, 0, len(lines)+4)
	contentLines = append(contentLines, headline, "", centeredURL, "")
	contentLines = append(contentLines, lines...)

	return successBoxStyle.Render(strings.Join(contentLines, "\n"))
}
