job "media" {
  datacenters = ["alo"]

  meta {
    uuid = uuidv4()
  }

  group "servers" {
    network {
      port "radarr" { static = 7878 }
      port "sonarr" { static = 8989 }
      port "bazarr" { to = 6767 }
      port "prowlarr" { static = 9696 }
      port "jellyseerr" { static = 5055 }
      port "flaresolverr" { static = 8191 }
      port "pms" { static = 32400 }
      port "qbt_ui" { static = 8080 }
      port "qbt_torrent" { static = 51413 }
    }

    task "radarr" {
      driver = "docker"

      config {
        image = "ghcr.io/hotio/radarr:latest"
        ports = [ "radarr" ]
        volumes = [
          "/data/services/media/radarr:/config",
          "/data/media:/data",
        ]
      }

      env {
        PUID = 1000
        PGID = 1000
        TZ = "Europe/Lisbon"
      }

      resources {
        cpu = 1000
      }

      service {
        name = "radarr"
        port = "radarr"

        tags = [
          "traefik.enable=true",
          "traefik.http.routers.radarr.entryPoints=websecure",
          "traefik.http.routers.radarr.middlewares=oidc-auth@file",
        ]
      }
    }

    task "sonarr" {
      driver = "docker"

      config {
        image = "ghcr.io/hotio/sonarr:latest"
        ports = [ "sonarr" ]
        volumes = [
          "/data/services/media/sonarr:/config",
          "/data/media:/data",
        ]
      }

      env {
        PUID = 1000
        PGID = 1000
        TZ = "Europe/Lisbon"
      }

      resources {
        cpu = 1000
        memory = 500
      }

      service {
        name = "sonarr"
        port = "sonarr"

        tags = [
          "traefik.enable=true",
          "traefik.http.routers.sonarr.entryPoints=websecure",
          "traefik.http.routers.sonarr.middlewares=oidc-auth@file",
        ]
      }
    }

#    task "bazarr" {
#      driver = "docker"
#
#      config {
#        image = "ghcr.io/hotio/bazarr:latest"
#        ports = [ "bazarr" ]
#        volumes = [
#          "/data/services/media/bazarr:/config",
#          "/data/media/media:/data/media",
#        ]
#      }
#
#      env {
#        PUID = 1000
#        PGID = 1000
#        TZ = "Europe/Lisbon"
#      }
#
#      resources {
#        cpu = 200
#        memory = 500
#      }
#
#      service {
#        name = "bazarr"
#        port = "bazarr"
#
#        tags = [
#          "traefik.enable=true",
#          "traefik.http.routers.bazarr.entryPoints=websecure",
#          "traefik.http.routers.bazarr.middlewares=oidc-auth@file",
#        ]
#      }
#    }

    task "prowlarr" {
      driver = "docker"

      config {
        image = "ghcr.io/hotio/prowlarr:latest"
        ports = [ "prowlarr" ]
        volumes = [
          "/data/services/media/prowlarr:/config",
        ]
      }

      env {
        PUID = 1000
        PGID = 1000
        TZ = "Europe/Lisbon"
      }

      resources {
        cpu = 200
      }

      service {
        name = "prowlarr"
        port = "prowlarr"

        tags = [
          "traefik.enable=true",
          "traefik.http.routers.prowlarr.entryPoints=websecure",
          "traefik.http.routers.prowlarr.middlewares=oidc-auth@file",
        ]
      }
    }

    task "jellyseerr" {
      driver = "docker"

      config {
        image = "fallenbagel/jellyseerr:latest"
        ports = [ "jellyseerr" ]
        volumes = [
          "/data/services/media/jellyseerr:/app/config",
        ]
      }

      env {
        TZ = "Europe/Lisbon"
      }

      resources {
        cpu = 200
      }

      service {
        name = "jellyseerr"
        port = "jellyseerr"

        tags = [
          "traefik.enable=true",
          "traefik.http.routers.jellyseerr.entryPoints=websecure",
          "traefik.http.routers.jellyseerr.middlewares=oidc-auth@file",
        ]
      }
    }

    task "flaresolverr" {
      driver = "docker"

      config {
        image = "ghcr.io/flaresolverr/flaresolverr:latest"
        ports = [ "flaresolverr" ]
      }

      env {
        LOG_LEVEL = "info"
        TZ = "Europe/Lisbon"
      }

      resources {
        cpu = 500
        memory = 1024
      }

      service {
        name = "flaresolverr"
        port = "flaresolverr"
      }
    }

    task "recyclarr" {
      driver = "docker"

      config {
        image = "ghcr.io/recyclarr/recyclarr:latest"
        volumes = [
          "/data/services/media/recyclarr:/config",
        ]
      }

      env {
        TZ = "Europe/Lisbon"
        CRON_SCHEDULE = "0 0 * * *"  # Daily at midnight
      }

      resources {
        cpu = 100
        memory = 256
      }
    }

    task "plex" {
      driver = "docker"

      config {
        image = "plexinc/pms-docker:latest"
        ports = [ "pms" ]
        network_mode = "host"
        volumes = [
          "/data/services/media/plex:/config",
          "/data/media/media:/data",
        ]
      }

      env {
        PLEX_UID = 1000
        PLEX_GID = 1000
        TZ = "Europe/Lisbon"
      }

      resources {
        cpu = 2000
        memory = 2000
      }

      service {
        name = "plex"
        port = "pms"

        tags = [
          "traefik.enable=true",
          "traefik.http.routers.plex.entryPoints=websecure",
          "traefik.http.routers.plex.middlewares=oidc-auth@file",
        ]
      }
    }

    task "qbittorrent" {
      driver = "docker"

      config {
        image = "lscr.io/linuxserver/qbittorrent:latest"
        ports = [
          "qbt_ui",
          "qbt_torrent",
        ]
        volumes = [
          "/data/services/media/qbittorrent:/config",
          "/data/media/torrents:/data/torrents",
        ]
      }

      env {
        PUID = 1000
        PGID = 1000
        TZ = "Europe/Lisbon"
        TORRENTING_PORT = 51413
      }

      resources {
        cpu = 2000
        memory = 1500
      }

      service {
        name = "torrent"
        port = "qbt_ui"

        tags = [
          "traefik.enable=true",
          "traefik.http.routers.torrent.entryPoints=websecure",
          "traefik.http.routers.torrent.middlewares=oidc-auth@file",
        ]
      }
    }

    task "keepalived" {
      driver = "docker"
      env {
        KEEPALIVED_INTERFACE = "eno1"
        KEEPALIVED_VIRTUAL_IPS = "192.168.1.102/24"
        KEEPALIVED_ROUTER_ID = "54" # defaults to 51
        KEEPALIVED_UNICAST_PEERS = ""
        KEEPALIVED_STATE       = "MASTER"
        KEEPALIVED_VIRTUAL_ROUTES = ""
      }
      config {
        # 2.2.8 wasn't starting, reason unknown
        image        = "visibilityspots/keepalived:2.2.7"
        network_mode = "host"
        privileged   = true
        cap_add      = ["NET_ADMIN", "NET_BROADCAST", "NET_RAW"]
      }
    }
  }
}
