// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

// Package ui provides lipgloss styles for terminal output.
package ui

import (
	"time"

	"github.com/charmbracelet/lipgloss"
	"github.com/klauspost/lctime"
)

// Style wraps lipgloss.Style to conditionally render based on output mode.
type Style struct {
	style lipgloss.Style
}

// Render applies the style to the given text, or returns plain text in plain mode.
func (s Style) Render(strs ...string) string {
	if IsPlain() {
		result := ""
		for _, str := range strs {
			result += str
		}

		return result
	}

	return s.style.Render(strs...)
}

// Style returns the underlying lipgloss.Style for advanced use.
func (s Style) Style() lipgloss.Style {
	return s.style
}

// Terminal output styles using ANSI colors for broad compatibility.
var (
	Success = Style{lipgloss.NewStyle().Foreground(lipgloss.Color("2"))} // green
	Warning = Style{lipgloss.NewStyle().Foreground(lipgloss.Color("3"))} // yellow
	Error   = Style{lipgloss.NewStyle().Foreground(lipgloss.Color("1"))} // red
	Bold    = Style{lipgloss.NewStyle().Bold(true)}
)

// Heading styles with backgrounds for contrast on any theme.
var (
	// H1 is the primary heading style (top-level items).
	H1 = Style{lipgloss.NewStyle().
		Bold(true).
		Foreground(lipgloss.Color("0")).
		Background(lipgloss.Color("4")).
		Padding(0, 1)}
	// H2 is the secondary heading style (nested items).
	H2 = Style{lipgloss.NewStyle().
		Bold(true).
		Foreground(lipgloss.Color("0")).
		Background(lipgloss.Color("6")).
		Padding(0, 1)}
)

// FormatDate formats a time.Time as a date string using the user's locale.
// Locale is auto-detected from LC_TIME, LC_ALL, or LANG environment variables.
func FormatDate(t time.Time) string {
	return lctime.Strftime("%x", t)
}

// TableHeaderStyle returns the style for table header rows.
// Returns bold style in interactive mode, plain in non-interactive.
func TableHeaderStyle() lipgloss.Style {
	if IsPlain() {
		return lipgloss.NewStyle()
	}

	return lipgloss.NewStyle().Bold(true)
}

// TableBorder returns the border style for tables.
// Returns a normal border in interactive mode, hidden in non-interactive.
func TableBorder() lipgloss.Border {
	if IsPlain() {
		return lipgloss.HiddenBorder()
	}

	return lipgloss.NormalBorder()
}
