- name: Add Jellyfin repository and key
  ansible.builtin.deb822_repository:
    name: 'jellyfin'
    types: 
      - deb
    uris: 'https://repo.jellyfin.org/debian'
    suites: 'trixie'
    components: 'main'
    signed_by: 'https://repo.jellyfin.org/debian/jellyfin_team.gpg.key'

- name: Install Jellyfin
  apt:
    name: jellyfin
    state: present

- name: Create Jellyfin user
  user:
    name: jellyfin
    # groups:
    #   - data
    append: true
    create_home: no
    shell: /bin/nologin
    update_password: on_create

- name: Copy over service file
  copy:
    src: jellyfin.service.conf
    dest: '/etc/systemd/system/jellyfin.service.d/jellyfin.service.conf'
    owner: root
    group: root
    mode: '644'
  notify: 
    - daemon-reload
    - restart jellyfin

- name: Copy over Environment file
  copy:
    src: jellyfin-defaults
    dest: '/etc/default/jellyfin'
    owner: root
    group: root
    mode: '644'
  notify: restart jellyfin

- name: Ensure Jellyfin service is running & enabled
  service:
    name: jellyfin
    state: started
    enabled: true

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