#!/usr/bin/env bash
set -euo pipefail

declare -A THEME=(
  ["ghostty.url"]="https://raw.githubusercontent.com/triimdev/invero.nvim/refs/heads/main/extras/ghostty/invero_day"
  ["ghostty.dir"]="$HOME/.config/ghostty/themes"
  ["ghostty.name"]="Invero Day"

  ["wezterm.url"]="https://raw.githubusercontent.com/triimdev/invero.nvim/refs/heads/main/extras/wezterm/invero_day.toml"
  ["wezterm.dir"]="$HOME/.config/wezterm/colors"
  ["wezterm.name"]="Invero Day.toml"
)

theme="${1:-}"

if [[ -z "$theme" ]]; then
  echo "Usage: $0 <theme>"
  echo "Available themes: $(printf '%s\n' "${!THEME[@]}" | cut -d. -f1 | sort -u | tr '\n' ' ')"
  exit 1
fi

if [[ -z "${THEME[$theme.url]+x}" ]]; then
  echo "Unknown theme '$theme'. Available: $(printf '%s\n' "${!THEME[@]}" | cut -d. -f1 | sort -u | tr '\n' ' ')"
  exit 1
fi

url="${THEME[$theme.url]}"
dir="${THEME[$theme.dir]}"
name="${THEME[$theme.name]}"
path="${dir}/${name}"

mkdir -p "$dir"

if curl -fsSL -o "$path" "$url"; then
  echo "Theme downloaded to $path"
else
  echo "Failed to download theme for '$theme'."
  exit 1
fi
