---
- name: Ensure Grafana is installed
  ansible.builtin.apt:
    deb: 'https://dl.grafana.com/grafana/release/12.3.3/grafana_12.3.3_21957728731_linux_amd64.deb'
  notify: 'restart grafana'

- name: Ensure data directory permissions are correct
  ansible.builtin.file:
    path: '{{ item }}'
    state: 'directory'
    owner: 'grafana'
    group: 'grafana'
  loop:
    - '{{ grafana_data_dir }}'
    - '{{ grafana_logs_dir }}'

- name: Ensure configuration directory is present
  ansible.builtin.file:
    path: '/etc/grafana'
    state: directory
    mode: '0755'

- name: Ensure config file is present
  ansible.builtin.template:
    src: 'grafana.ini.j2'
    dest: '/etc/grafana/grafana.ini'
    mode: '0644'
    owner: 'root'
    group: 'root'
  notify: 'restart grafana'

- name: Ensure service is enabled
  ansible.builtin.service:
    name: 'grafana-server'
    enabled: true

- name: Ensure Caddyfile is present
  ansible.builtin.copy:
    src: 'grafana.Caddyfile'
    dest: '/etc/caddy/grafana.Caddyfile'
    owner: root
    group: root
    mode: '0644'
  notify: reload caddy
