#!/usr/bin/env bash

DEST=${1:-/etc/skel}
# Install lazycommit
# go install github.com/m7medvision/lazycommit@latest

# # Add once – usually already present if you installed Go via official method
# if [[ ":$PATH:" != *":$HOME/go/bin:"* ]]; then
#   echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.zshrc
#   export PATH="$HOME/go/bin:$PATH"
# fi

# Create the configuration directory
# LAZYCOMMIT_HOME=${DEST}/.config/
# mkdir -p "${LAZYCOMMIT_HOME}/lazycommit"

# Create the configuration file with default settings
cat >"${DEST}/.config/.lazycommit.yaml" <<'EOF'
active_provider: anthropic # default is copilot if a GitHub token is found
providers:
  copilot:
    api_key: "$GITHUB_TOKEN"   # Uses GitHub token; token is exchanged internally
    model: "gpt-4o"            # or "openai/gpt-4o"; both accepted
    # endpoint_url: "https://api.githubcopilot.com"  # Optional - uses default if not specified
  openai:
    api_key: "$OPENAI_API_KEY"
    model: "gpt-4o"
    # endpoint_url: "https://api.openai.com/v1"  # Optional - uses default if not specified
  anthropic:
    model: "claude-haiku-4-5"  # Uses Claude Code CLI - no API key needed
    num_suggestions: 10        # Number of commit suggestions to generate
EOF

# Prompt templates and message configurations.
cat >"${DEST}/.config/.lazycommit.prompts.yaml" <<EOF
language: English # commit message language (e.g., "English", "Arabic", "Korean")
system_message: "You are a helpful assistant that generates git commit messages, and pull request titles."
commit_message_template: "Based on the following git diff, generate 10 conventional commit messages. Each message should be on a new line, without any numbering or bullet points:\n\n%s"
pr_title_template: "Based on the following git diff, generate 10 pull request title suggestions. Each title should be on a new line, without any numbering or bullet points:\n\n%s"
EOF

# NB We use ${HOME}/.config/lazygit/extras for extending the lazygit/config.yml
# # ~/.config/lazygit/
# ├── config.yml          # minimal base + includes comment explaining the rest
# └── extras/
#     ├── 01-theme.yml
#     ├── 10-editor-nvim.yml
#     ├── 20-custom-commands-ai.yml
#     └── 30-pager-delta.yml
# then:
## In ~/.zshrc or similar
# alias lg='LG_CONFIG_FILE=$$   (ls -1 ~/.config/lazygit/extras/*.yml | tr "\n" "," | sed "s/,   $$//"),~/.config/lazygit/config.yml lazygit'
# Add custom command to lazygit: ${HOME}/.config/lazygit/config.yml
#
# customCommands:
# - key: "<c-a>" # ctrl + a
# description: "Pick AI commit"
# command: 'echo "{{.Form.Msg}}" > .git/COMMIT_EDITMSG && nvim .git/COMMIT_EDITMSG && [ -s .git/COMMIT_EDITMSG ] && git commit -F .git/COMMIT_EDITMSG || echo "Commit message is empty, commit aborted."'
# context: "files"
# subprocess: true
# prompts:
# - type: "menuFromCommand"
# title: "AI Commits"
# key: "Msg"
# command: "bunx @m7medvision/lazycommit@latest"
# filter: '^(?P<number>\d+)\.\s(?P<message>.+)$'
# valueFormat: "{{ .message }}"
# labelFormat: "{{ .number }}: {{ .message | green }}"

# Check for the the existance of lazygit config.yml

# yq e -i '.customCommands += [{"key": "<c-a>", "description": "Pick AI commit", "context": "files", "subprocess": true, "loadingText": "Generating commit message...", "prompts": [{"type": "menuFromCommand", "title": "AI Generated Commits", "key": "Msg", "command": "bunx @m7medvision/lazycommit@latest --list", "filter": "^(?P<number>\\d+)\\.\\s(?P<message>.+)$", "valueFormat": "{{ .message }}", "labelFormat": "{{ .number }}: {{ .message | green }}"}], "command": "echo \"{{index .PromptResponses 0}}\" > .git/COMMIT_EDITMSG && nvim .git/COMMIT_EDITMSG && if [ -s .git/COMMIT_EDITMSG ]; then git commit -F .git/COMMIT_EDITMSG; else echo \"Commit message is empty, commit aborted.\" && rm .git/COMMIT_EDITMSG; fi"}]' \
#   "${HOME}/.config/lazygit/config.yml"

# Write a confguration for lazycommmit in lazygit.
CONFIG_ROOT="${DEST}/.config/lazygit"
EXTRAS_DIR="${CONFIG_ROOT}/extras"
mkdir -p "${EXTRAS_DIR}"

conf_print_LC_LD-Config() {
  cat <<'EOF'
customCommands:
  # Quick commit without editing (direct accept)
  - key: "<c-a>"
    description: "Pick AI commit & commit immediately"
    context: "files"
    loadingText: "Generating commit suggestions..."
    subprocess: true
    command: 'git commit -m "{{.Form.Msg}}"'
    prompts:
      - type: "menuFromCommand"
        title: "AI Generated Commits"
        key: "Msg"
        command: "lazycommit commit"
        filter: '^(?P<raw>.+)$'
        valueFormat: "{{ .raw }}"
        labelFormat: "{{ .raw | green }}"

  # Edit before commit (your original style, but updated)
  - key: "<c-b>"
    description: "Pick AI commit (edit before commit)"
    context: "files"
    loadingText: "Generating commit suggestions..."
    subprocess: true
    command: |
      bash -c 'msg="{{.Form.Msg}}"
      echo "$msg" > .git/COMMIT_EDITMSG
      ${EDITOR:-nvim} .git/COMMIT_EDITMSG
      if [ -s .git/COMMIT_EDITMSG ]; then
        git commit -F .git/COMMIT_EDITMSG
      else
        echo "Commit message empty → aborted."
        rm -f .git/COMMIT_EDITMSG
      fi'
    prompts:
      - type: "menuFromCommand"
        title: "AI Generated Commits"
        key: "Msg"
        command: "lazycommit commit"
        filter: '^(?P<raw>.+)$'
        valueFormat: "{{ .raw }}"
        labelFormat: "{{ .raw | green }}"
EOF
}
conf_print_LC_LD-Config | tee "${EXTRAS_DIR}/20-lazycommit.yml"

# Verify the installation
echo "Current PATH entries:"
echo "$PATH" | tr ':' '\n' | grep '/go/bin'

# Test lazycommit
# lazycommit commit
