;;; $DOOMDIR/config.el -*- lexical-binding: t; -*-

;; Place your private configuration here! Remember, you do not need to run 'doom
;; sync' after modifying this file!


;; Some functionality uses this to identify you, e.g. GPG configuration, email
;; clients, file templates and snippets. It is optional.
;; (setq user-full-name "John Doe"
;;       user-mail-address "john@doe.com")

;; Doom exposes five (optional) variables for controlling fonts in Doom:
;;
;; - `doom-font' -- the primary font to use
;; - `doom-variable-pitch-font' -- a non-monospace font (where applicable)
;; - `doom-big-font' -- used for `doom-big-font-mode'; use this for
;;   presentations or streaming.
;; - `doom-symbol-font' -- for symbols
;; - `doom-serif-font' -- for the `fixed-pitch-serif' face
;;
;; See 'C-h v doom-font' for documentation and more examples of what they
;; accept. For example:
;;
;;(setq doom-font (font-spec :family "Fira Code" :size 12 :weight 'semi-light)
;;      doom-variable-pitch-font (font-spec :family "Fira Sans" :size 13))
;;
;; If you or Emacs can't find your font, use 'M-x describe-font' to look them
;; up, `M-x eval-region' to execute elisp code, and 'M-x doom/reload-font' to
;; refresh your font settings. If Emacs still can't find your font, it likely
;; wasn't installed correctly. Font issues are rarely Doom issues!
(setq doom-font (font-spec :family "Fira Code" :size 16))

;; Auto-install nerd-icons fonts if they're missing
(defun my/ensure-nerd-icons-fonts ()
  "Check if nerd-icons fonts are installed and install them if missing."
  (when (display-graphic-p)
    (unless (find-font (font-spec :name "Symbols Nerd Font Mono"))
      (when (fboundp 'nerd-icons-install-fonts)
        (nerd-icons-install-fonts t)))))

(add-hook 'doom-init-ui-hook #'my/ensure-nerd-icons-fonts)

;; There are two ways to load a theme. Both assume the theme is installed and
;; available. You can either set `doom-theme' or manually load a theme with the
;; `load-theme' function. This is the default:
(setq doom-theme 'doom-tokyo-night)

;; This determines the style of line numbers in effect. If set to `nil', line
;; numbers are disabled. For relative line numbers, set this to `relative'.
(setq display-line-numbers-type t)

;; If you use `org' and don't want your org files in the default location below,
;; change `org-directory'. It must be set before org loads!
(setq org-directory "~/org/")
(after! org
  ;; Skip recurring events past their CALDAV_UNTIL date
  ;; org-caldav ignores UNTIL from RRULE, so we store it as a property
  ;; and filter here in the agenda
  (defun my/skip-if-past-until ()
    "Return non-nil if entry has CALDAV_UNTIL and current date is past it."
    (let ((until-str (org-entry-get nil "CALDAV_UNTIL")))
      (when (and until-str
                 (string-match "^\\([0-9]\\{4\\}\\)\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)" until-str))
        (let* ((until-year (string-to-number (match-string 1 until-str)))
               (until-month (string-to-number (match-string 2 until-str)))
               (until-day (string-to-number (match-string 3 until-str)))
               (until-time (encode-time 0 0 0 until-day until-month until-year))
               (today (current-time)))
          (when (time-less-p until-time today)
            (org-end-of-subtree t))))))

  (setq org-agenda-span 'week
        org-agenda-start-with-log-mode t
        my-agenda-dirs '("projects" "roam")
        org-agenda-files (cons org-directory (mapcan (lambda (x) (directory-files-recursively
                                                                  (expand-file-name x org-directory)
                                                                  "\.org$"))
                                                     my-agenda-dirs))
        org-log-done 'time
        org-agenda-skip-function-global #'my/skip-if-past-until
        org-agenda-custom-commands '(("n" "Agenda"
                                      ((agenda "")
                                       (tags-todo "-someday-recurring")))
                                     ("s" "Someday Items"
                                      ((tags-todo "+someday"))))
        org-todo-keywords '((sequence "TODO(t)" "IN-PROGRESS(p)" "WAIT(w)" "|" "DONE(d)" "KILL(k)"))
        org-journal-file-type 'weekly
        org-journal-file-format "%Y-%m-%d.org"
        org-capture-templates
          '(("t" "Todo" entry (file+headline "~/org/todo.org" "Inbox")
             "* TODO %? \n %i \n%a" :prepend t)))
  ;; Make blocked tasks more visible in agenda (they have subtasks to do!)
  (custom-set-faces!
    '(org-agenda-dimmed-todo-face :foreground "#bb9af7" :weight normal)))

(map! :after org-agenda
      :map org-agenda-mode-map
      :localleader
      (:prefix ("v" . "view")
       "d" #'org-agenda-day-view
       "w" #'org-agenda-week-view))

;; org-caldav: Sync Org entries with Nextcloud CalDAV
;; Setup requirements:
;; 1. Create Nextcloud app password: Settings -> Security -> Devices & sessions
;; 2. Store in rbw: rbw add nextcloud-caldav (put app password as the secret)
;; 3. Run: doom sync
;; 4. Test: M-x my/org-caldav-sync-with-rbw (or SPC o a s)
;;
;; Note: Conflict resolution is "Org always wins" - treat Org as source of truth
;; for entries that originated in Org.

;; Define sync wrapper before use-package (so keybinding works)
(defun my/org-caldav-sync-with-rbw ()
  "Run org-caldav-sync with credentials from rbw embedded in URL."
  (interactive)
  (require 'org)
  (require 'org-caldav)
  (let* ((password (my/get-rbw-password "nextcloud-caldav"))
         ;; Embed credentials in URL (url-encode password in case of special chars)
         (encoded-pass (url-hexify-string password)))
    (setq org-caldav-url
          (format "https://johno:%s@nextcloud.johnogle.info/remote.php/dav/calendars/johno"
                  encoded-pass))
    (org-caldav-sync)))

(use-package! org-caldav
  :after org
  :commands (org-caldav-sync my/org-caldav-sync-with-rbw)
  :init
  (map! :leader
        (:prefix ("o" . "open")
         (:prefix ("a" . "agenda/calendar")
          :desc "Sync CalDAV" "s" #'my/org-caldav-sync-with-rbw)))
  :config
  ;; Nextcloud CalDAV base URL (credentials added dynamically by sync wrapper)
  (setq org-caldav-url "https://nextcloud.johnogle.info/remote.php/dav/calendars/johno")

  ;; Timezone for iCalendar export
  (setq org-icalendar-timezone "America/Los_Angeles")

  ;; Sync state storage (in org directory for multi-machine sync)
  (setq org-caldav-save-directory (expand-file-name ".org-caldav/" org-directory))

  ;; Backup file for entries before modification
  (setq org-caldav-backup-file (expand-file-name ".org-caldav/backup.org" org-directory))

  ;; Limit past events to 30 days (avoids uploading years of scheduled tasks)
  (setq org-caldav-days-in-past 30)

  ;; Sync behavior: bidirectional by default
  (setq org-caldav-sync-direction 'twoway)

  ;; What changes from calendar sync back to Org (conservative: title and timestamp only)
  (setq org-caldav-sync-changes-to-org 'title-and-timestamp)

  ;; Deletion handling: never auto-delete to prevent accidental mass deletion
  (setq org-caldav-delete-calendar-entries 'never)
  (setq org-caldav-delete-org-entries 'never)

  ;; Enable TODO/VTODO sync
  (setq org-icalendar-include-todo 'all)
  (setq org-caldav-sync-todo t)

  ;; Map VTODO percent-complete to org-todo-keywords
  ;; Format: (PERCENT "KEYWORD") - percent thresholds map to states
  (setq org-caldav-todo-percent-states
        '((0 "TODO")
          (25 "WAIT")
          (50 "IN-PROGRESS")
          (100 "DONE")
          (100 "KILL")))

  ;; Allow export with broken links (mu4e links can't be resolved during export)
  (setq org-export-with-broken-links 'mark)

  ;; Calendar-specific configuration
  (setq org-caldav-calendars
        '(;; Personal calendar: two-way sync with family-shared Nextcloud calendar
          (:calendar-id "personal"
           :inbox "~/org/personal-calendar.org"
           :files ("~/org/personal-calendar.org"))

          ;; Tasks calendar: one-way sync (org → calendar only)
          ;; SCHEDULED/DEADLINE items from todo.org push to private Tasks calendar.
          ;; No inbox = no download from calendar (effectively one-way).
          ;; Note: Create 'tasks' calendar in Nextcloud first, keep it private.
          (:calendar-id "tasks"
           :files ("~/org/todo.org"))))

  ;; Handle UNTIL in recurring events
  ;; org-caldav ignores UNTIL from RRULE - events repeat forever.
  ;; This advice extracts UNTIL and stores it as a property for agenda filtering.
  (defun my/org-caldav-add-until-property (orig-fun eventdata-alist)
    "Advice to store CALDAV_UNTIL property for recurring events."
    (let ((result (funcall orig-fun eventdata-alist)))
      (let* ((rrule-props (alist-get 'rrule-props eventdata-alist))
             (until-str (cadr (assoc 'UNTIL rrule-props)))
             (summary (alist-get 'summary eventdata-alist)))
        ;; Debug: log what we're seeing
        (message "CALDAV-DEBUG: %s | rrule-props: %S | until: %s"
                 (or summary "?") rrule-props until-str)
        (when until-str
          (save-excursion
            (org-back-to-heading t)
            (org-entry-put nil "CALDAV_UNTIL" until-str))))
      result))

  (advice-add 'org-caldav-insert-org-event-or-todo
              :around #'my/org-caldav-add-until-property)
  )

(defun my/get-rbw-password (alias &optional no-error)
  "Return the password for ALIAS via rbw, unlocking the vault only if needed.
If NO-ERROR is non-nil, return nil instead of signaling an error when
rbw is unavailable or the entry is not found."
  (if (not (executable-find "rbw"))
      (if no-error
          nil
        (user-error "rbw: not installed or not in PATH"))
    (let* ((cmd (format "rbw get %s 2>/dev/null" (shell-quote-argument alias)))
           (output (string-trim (shell-command-to-string cmd))))
      (if (string-empty-p output)
          (if no-error
              nil
            (user-error "rbw: no entry found for '%s' - run: rbw add %s" alias alias))
        output))))

(after! gptel
  :config
  (setq! gptel-api-key (my/get-rbw-password "openai-api-key-chatgpt-el" t)
         gptel-default-mode 'org-mode
         gptel-use-tools t
         gptel-confirm-tool-calls 'always
         gptel-include-reasoning 'ignore
         gptel-model "qwen3:30b")

  ;; Set default backend to be Ollama-Local
  (setq! gptel-backend
         (gptel-make-ollama "Ollama-Local"
           :host "localhost:11434"
           :stream t
           :models '(deepseek-r1 deepseek-r1-fullctx qwen3:30b qwen3:4b llama3.1 qwen2.5-coder mistral-nemo gpt-oss)))

  ;; Define custom tools
  (gptel-make-tool
   :name "run_shell_command"
   :description "Execute shell commands and return output. Use this to run system commands, check file contents, or perform system operations."
   :function (lambda (command)
               (condition-case err
                   (shell-command-to-string command)
                 (error (format "Error running command: %s" (error-message-string err)))))
   :args (list '(:name "command" :type "string" :description "Shell command to execute")))

  (gptel-make-tool
   :name "read_file"
   :description "Read the contents of a file and return as text"
   :function (lambda (filepath)
               (condition-case err
                   (with-temp-buffer
                     (insert-file-contents (expand-file-name filepath))
                     (buffer-string))
                 (error (format "Error reading file %s: %s" filepath (error-message-string err)))))
   :args (list '(:name "filepath" :type "string" :description "Path to the file to read")))

  (gptel-make-tool
   :name "list_directory"
   :description "List contents of a directory"
   :function (lambda (dirpath)
               (condition-case err
                   (mapconcat 'identity 
                              (directory-files (expand-file-name dirpath) nil "^[^.]")
                              "\n")
                 (error (format "Error listing directory %s: %s" dirpath (error-message-string err)))))
   :args (list '(:name "dirpath" :type "string" :description "Directory path to list"))))

(use-package! pi-coding-agent
  :commands (pi-coding-agent pi-coding-agent-toggle)
  :init
  (defalias 'pi 'pi-coding-agent)
  (map! :leader
        (:prefix ("o" . "open")
         :desc "Pi Coding Agent" "p" #'pi-coding-agent))
  :config
  ;; Tree-sitter grammars are managed by Nix (treesit-grammars.with-all-grammars),
  ;; so suppress the auto-install prompt
  (setq pi-coding-agent-essential-grammar-action 'warn))

(use-package! claude-code-ide
  :commands (claude-code-ide-menu claude-code-ide-open-here)
  :init
  (map! :leader
        (:prefix ("o" . "open")
         :desc "Claude Code IDE" "c" #'claude-code-ide-menu))
  :config
  (claude-code-ide-emacs-tools-setup)
  (setq claude-code-ide-cli-path "claude"
        claude-code-ide-cli-extra-flags "--dangerously-skip-permissions"
        claude-code-ide-focus-claude-after-ediff t
        claude-code-ide-focus-on-open t
        claude-code-ide-show-claude-window-in-ediff t
        claude-code-ide-switch-tab-on-ediff t
        claude-code-ide-use-ide-diff t
        claude-code-ide-use-side-window t
        claude-code-ide-window-height 20
        claude-code-ide-window-side 'right
        claude-code-ide-window-width 90))

(use-package! beads
  :commands (beads)
  :init
  (map! :leader
        (:prefix ("o" . "open")
         (:prefix ("B" . "beads")
          :desc "List issues"      "B" (cmd! (require 'beads) (beads-list))
          :desc "Project issues"   "p" (cmd! (require 'beads) (beads-project-list))
          :desc "Activity feed"    "a" (cmd! (require 'beads) (beads-activity))
          :desc "Stale issues"     "s" (cmd! (require 'beads) (beads-stale))
          :desc "Orphaned issues"  "o" (cmd! (require 'beads) (beads-orphans))
          :desc "Find duplicates"  "d" (cmd! (require 'beads) (beads-duplicates))
          :desc "Lint issues"      "l" (cmd! (require 'beads) (beads-lint))))))

(after! gptel
  (require 'gptel-tool-library)
  (setq gptel-tool-library-use-maybe-safe t
        gptel-tool-library-use-unsafe t)
  (dolist (module '("bbdb" "buffer" "elisp" "emacs" "gnus" "os" "search-and-replace" "url"))
    (gptel-tool-library-load-module module)))

;; mu4e email configuration
;; Add NixOS mu4e to load-path (installed via mu.mu4e package)
(when-let ((mu-path (executable-find "mu")))
  (add-to-list 'load-path
               (expand-file-name "../share/emacs/site-lisp/mu4e"
                                 (file-name-directory mu-path))))

(after! mu4e
  ;; User identity
  (setq user-mail-address "john@ogle.fyi"
        user-full-name "John Ogle")

  ;; Maildir location (no account prefix - single account)
  (setq mu4e-maildir "~/Mail"
        mu4e-attachment-dir "~/Downloads")

  ;; Folder config (matches ~/Mail/INBOX, ~/Mail/Sent, etc.)
  (setq mu4e-sent-folder "/Sent"
        mu4e-drafts-folder "/Drafts"
        mu4e-trash-folder "/Trash"
        mu4e-refile-folder "/Archive")

  ;; Shortcuts for common folders
  (setq mu4e-maildir-shortcuts
        '((:maildir "/INBOX"   :key ?i)
          (:maildir "/Archive" :key ?a)
          (:maildir "/Sent"    :key ?s)
          (:maildir "/Trash"   :key ?t)))

  ;; Behavior settings
  (setq mu4e-get-mail-command "mbsync -a"
        mu4e-update-interval 300  ; 5 minutes (matches systemd timer)
        mu4e-change-filenames-when-moving t  ; required for mbsync
        mu4e-headers-date-format "%Y-%m-%d"
        mu4e-headers-time-format "%H:%M")

  ;; Sending mail via msmtp
  ;; NOTE: message-sendmail-f-is-evil and --read-envelope-from are required
  ;; to prevent msmtp from stripping the email body when processing headers.
  ;; Without these, multipart messages (especially from org-msg) may arrive
  ;; with empty bodies.
  (setq sendmail-program (executable-find "msmtp")
        send-mail-function #'message-send-mail-with-sendmail
        message-send-mail-function #'message-send-mail-with-sendmail
        message-sendmail-f-is-evil t
        message-sendmail-extra-arguments '("--read-envelope-from")
        message-sendmail-envelope-from 'header))

;; Whenever you reconfigure a package, make sure to wrap your config in an
;; `after!' block, otherwise Doom's defaults may override your settings. E.g.
;;
;;   (after! PACKAGE
;;     (setq x y))
;;
;; The exceptions to this rule:
;;
;;   - Setting file/directory variables (like `org-directory')
;;   - Setting variables which explicitly tell you to set them before their
;;     package is loaded (see 'C-h v VARIABLE' to look up their documentation).
;;   - Setting doom variables (which start with 'doom-' or '+').
;;
;; Here are some additional functions/macros that will help you configure Doom.
;;
;; - `load!' for loading external *.el files relative to this one
;; - `use-package!' for configuring packages
;; - `after!' for running code after a package has loaded
;; - `add-load-path!' for adding directories to the `load-path', relative to
;;   this file. Emacs searches the `load-path' when you load packages with
;;   `require' or `use-package'.
;; - `map!' for binding new keys
;;
;; To get information about any of these functions/macros, move the cursor over
;; the highlighted symbol at press 'K' (non-evil users must press 'C-c c k').
;; This will open documentation for it, including demos of how they are used.
;; Alternatively, use `C-h o' to look up a symbol (functions, variables, faces,
;; etc).
;;
;; You can also try 'gd' (or 'C-c c d') to jump to their definition and see how
;; they are implemented.
