# =============================================================================
# TASKS - DOCKER SERVICE
# =============================================================================
#
# SERVICE OVERVIEW:
# - Container: tasks
# - Image: linuxserver/tautulli:latest
# - Configuration: ansible/deploy_arr_suite_updated.yml
#
# DISASTER RECOVERY PRIORITY: MEDIUM
# - Recovery Time Objective (RTO): 1 hour
# - Recovery Point Objective (RPO): 24 hours
#
# BACKUP REQUIREMENTS:
# - Configuration: Docker volumes and bind mounts
# - Data: Persistent volumes (if any)
# - Frequency: Daily for critical services, weekly for others
#
# DEPENDENCIES:
# - Docker daemon running
# - Network connectivity
# - Storage volumes accessible
# - Required environment variables set
#
# RECOVERY PROCEDURE:
# 1. Ensure dependencies are met
# 2. Restore configuration and data from backups
# 3. Deploy using: docker-compose -f deploy_arr_suite_updated.yml up -d
# 4. Verify service functionality
# 5. Update monitoring and documentation
#
# =============================================================================

- name: Deploy ARR Suite with Ansible
  hosts: all
  become: yes
  tasks:
    - name: Ensure required directories exist
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        mode: '0755'
        owner: vish
        group: vish
      loop:
        - /home/vish/docker/tautulli
        - /home/vish/docker/prowlarr
        - /home/vish/docker/flaresolverr
        - /home/vish/docker/sabnzbd
        - /home/vish/docker/sonarr
        - /home/vish/docker/lidarr
        - /home/vish/docker/radarr
        - /home/vish/docker/readarr
        - /home/vish/docker/bazarr
        - /home/vish/docker/whisparr
        - /home/vish/docker/plex
        - /home/vish/docker/jellyseerr
        - /home/vish/data/usenet
        - /home/vish/data/media
        - /home/vish/data

    - name: Install Docker
      ansible.builtin.package:
        name: docker
        state: present

    - name: Install Docker Python module
      ansible.builtin.pip:
        name: docker
        state: present

    - name: Start Docker service
      ansible.builtin.service:
        name: docker
        state: started
        enabled: yes

    - name: Deploy Docker network (synobridge)
      community.docker.docker_network:
        name: synobridge

    - name: Deploy Tautulli
      community.docker.docker_container:
        name: tautulli
        image: linuxserver/tautulli:latest
        env:
          PUID: "1000"
          PGID: "1000"
          TZ: "America/Los_Angeles"
          UMASK: "022"
        volumes:
          - /home/vish/docker/tautulli:/config
        ports:
          - "8181:8181"
        network_mode: synobridge
        security_opts:
          - no-new-privileges:true
        restart_policy: always

    - name: Deploy Prowlarr
      community.docker.docker_container:
        name: prowlarr
        image: linuxserver/prowlarr:latest
        env:
          PUID: "1000"
          PGID: "1000"
          TZ: "America/Los_Angeles"
          UMASK: "022"
        volumes:
          - /home/vish/docker/prowlarr:/config
        ports:
          - "9696:9696"
        network_mode: synobridge
        security_opts:
          - no-new-privileges:true
        restart_policy: always

    - name: Deploy Plex
      community.docker.docker_container:
        name: plex
        image: linuxserver/plex:latest
        env:
          PUID: "1000"
          PGID: "1000"
          TZ: "America/Los_Angeles"
          UMASK: "022"
          VERSION: "docker"
          PLEX_CLAIM: ""
        volumes:
          - /home/vish/docker/plex:/config
          - /home/vish/data/media:/data/media
        devices:
          - /dev/dri:/dev/dri
        network_mode: host
        security_opts:
          - no-new-privileges:true
        restart_policy: always

# =============================================================================
# BASIC DISASTER RECOVERY COMMANDS
# =============================================================================
#
# BACKUP:
# docker-compose -f deploy_arr_suite_updated.yml down
# tar -czf backup-tasks-$(date +%Y%m%d).tar.gz [volume-paths]
#
# RESTORE:
# tar -xzf backup-tasks-[date].tar.gz
# docker-compose -f deploy_arr_suite_updated.yml up -d
#
# VERIFY:
# docker-compose -f deploy_arr_suite_updated.yml ps
# docker logs tasks
#
# =============================================================================
