// 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 (
	"os"
	"time"

	"github.com/charmbracelet/lipgloss"
)

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

// dateFormat holds the date format string derived from locale.
var dateFormat = initDateFormat()

// initDateFormat determines the date format based on LC_TIME or LANG.
func initDateFormat() string {
	locale := os.Getenv("LC_TIME")
	if locale == "" {
		locale = os.Getenv("LANG")
	}

	// US English uses month-first; most other locales use day-first or ISO
	switch {
	case len(locale) >= 2 && locale[:2] == "en" && len(locale) >= 5 && locale[3:5] == "US":
		return "01/02/2006"
	case len(locale) >= 2 && locale[:2] == "en":
		return "02/01/2006"
	default:
		return "2006-01-02" // ISO 8601 as sensible default
	}
}

// FormatDate formats a time.Time as a date string using the user's locale.
func FormatDate(t time.Time) string {
	return t.Format(dateFormat)
}
