(use-package org-roam
  :init (setq org-roam-v2-ack t) ;; Acknowledge V2 upgrade
  :after org
  :config
  (org-roam-db-autosync-enable)
  (require 'org-roam-dailies)
  :bind  (
   ("C-c n f" . org-roam-node-find)
   ("C-c n g" . org-roam-graph)
   ("C-c n r" . org-roam-node-random)
   ("C-c n h" . org-roam-node-convert-headline)
   ("C-c n i" . org-roam-node-insert)
   ("C-c n o" . org-id-get-create)
   ("C-c n t" . org-roam-tag-add)
   ("C-c n a" . org-roam-alias-add)
   ("C-c n l" . org-roam-buffer-display-dedicated)
   )
  )

(setq org-roam-directory (expand-file-name (concat org-directory "notes")))
(setq org-roam-dailies-directory (expand-file-name (concat org-directory "daily")))

(use-package sqlite3)
(require 'sqlite3)

(setq org-roam-file-exclude-regexp "^[.][.]?/")

(setq org-roam-mode-sections
      (list #'org-roam-backlinks-section
            #'org-roam-reflinks-section
            #'org-roam-unlinked-references-section 
            )
      )

(defun my/org-roam-node-has-tag (node tag)
  "Filter function to check if the given NODE has the specified TAG."
  (member tag (org-roam-node-tags node))
  )

(defun my/org-roam-node-find-by-tag ()
  "Find and open an Org-roam node based on a specified tag."
  (interactive)
  (let ((tag (read-string "Enter tag: ")))
    (org-roam-node-find nil nil (lambda (node) (my/org-roam-node-has-tag node tag))))
  )

(setq org-roam-capture-templates
      '(
        ("L" "link" plain
         (function org-roam--capture-get-point)
         "%?"
         :file-name "web/%<%Y-%m-%dT%H%M%S>.org"
         :head "#+TITLE: ${title}\n#+CREATED: %<%Y-%m-%dT%H%M%S>"
         :immediate-finish t
         :unnarrowed t
         )

        ("h" "hugo post" plain
         "%?"
         :target (file+head "posts/${slug}.org"
                            "#+TITLE: ${title}\n#+DATE: %U\n#+HUGO_BASE_DIR: ~/gharbeia.net\n#+HUGO_SECTION: ./posts\n#+HUGO_AUTO_SET_LASTMOD: t\n#+HUGO_TAGS: article\n#+HUGO_DRAFT: true\n")
         :immediate-finish t
         :unnarrowed t
         )

        ("p" "person" plain
         "%?"
         :if-new (file+head "people/${slug}.org"
                           "#+TITLE: ${title}")
        :immediate-finish t
        :unnarrowed t
        )
       )
     )

(setq org-roam-dailies-capture-templates
      '(
        ("d" "daily" plain
         ""
        :target ("file+heaed %<%Y-%m-%d>.org"  "#+title: %<%Y-%m-%d>\n\n")
        :immediate-finish t
        )
       )
     )

(defun my/org-move-entry-to-daily-notes ()
  "Move the current org-mode headline to the daily notes file based on its :CREATED: property."
  (interactive)
  (let*
      (
       (created-prop (org-entry-get nil "CREATED"))
       (created-date (when created-prop
                       (org-parse-time-string created-prop)))
       (year (nth 5 created-date)) ; Extract year (6th element)
       (month (nth 4 created-date)) ; Extract month (5th element)
       (day (nth 3 created-date)) ; Extract day (4th element)
       (target-date (format "%04d-%02d-%02d" year month day)) ; Format date string
       (target-file (org-roam-dailies-goto target-date)) 
       )
    (when target-file
      (org-cut-subtree)
      (find-file target-file)
      (goto-char (point-max))
      (unless (bolp) (newline))
      (org-paste-subtree)
      )
    )
)
