{
  "$schema": "./.schemas/page.schema.json",
  "title": "Viewer",
  "icon": "person",
  "sidebar": false,
  "params": {
    "user": {
      "type": "integer",
      "example": "1001"
    }
  },
  "title_from": {
    "ref": "u",
    "value": "user.response.data.friendly_name"
  },
  "period": {
    "choices": [
      {
        "id": "30",
        "label": "30 days",
        "last_days": 30
      },
      {
        "id": "7",
        "label": "7 days",
        "last_days": 7
      },
      {
        "id": "90",
        "label": "90 days",
        "last_days": 90
      }
    ]
  },
  "sources": {
    "u": {
      "url": "http://tautulli.local:8181",
      "secret": "tautulli",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "15m",
      "requests": {
        "user": "/api/v2?cmd=get_user&user_id={param.user}",
        "plays": "/api/v2?cmd=get_plays_by_date&time_range={period.id}&user_id={param.user}",
        "time": "/api/v2?cmd=get_plays_by_date&time_range={period.id}&y_axis=duration&user_id={param.user}",
        "all": "/api/v2?cmd=get_user_watch_time_stats&user_id={param.user}&query_days=0",
        "players": "/api/v2?cmd=get_user_player_stats&user_id={param.user}",
        "history": "/api/v2?cmd=get_history&user_id={param.user}&length=7&include_activity=0"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "user.response.result",
              "equals": "error"
            },
            {
              "path": "plays.response.result",
              "equals": "error"
            },
            {
              "path": "time.response.result",
              "equals": "error"
            },
            {
              "path": "all.response.result",
              "equals": "error"
            },
            {
              "path": "players.response.result",
              "equals": "error"
            },
            {
              "path": "history.response.result",
              "equals": "error"
            }
          ]
        },
        "message": {
          "if": {
            "path": "user.response.message",
            "exists": true
          },
          "then": {
            "template": "{user.response.message}"
          },
          "else": {
            "template": "Tautulli answered with an error"
          }
        }
      },
      "derive": [
        {
          "name": "days",
          "value": "plays.response.data.categories"
        },
        {
          "name": "per_day",
          "value": {
            "first": "data",
            "of": "plays.response.data.series",
            "where": {
              "path": "name",
              "equals": "Total"
            }
          }
        },
        {
          "name": "secs_per_day",
          "value": {
            "first": "data",
            "of": "time.response.data.series",
            "where": {
              "path": "name",
              "equals": "Total"
            }
          }
        },
        {
          "name": "plays",
          "value": {
            "sum": "$",
            "of": "per_day",
            "default": 0
          }
        },
        {
          "name": "watched",
          "value": {
            "sum": "$",
            "of": "secs_per_day",
            "default": 0
          }
        },
        {
          "name": "ever",
          "value": {
            "first": "$",
            "of": "all.response.data",
            "where": {
              "path": "query_days",
              "equals": 0
            }
          }
        },
        {
          "name": "devices",
          "value": {
            "join": "player_name",
            "of": "players.response.data",
            "limit": 3
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "plays",
      "type": "sparkline",
      "label": "Plays per day",
      "unit": "plays",
      "points": [
        0,
        0
      ],
      "change": false,
      "source": {
        "ref": "u",
        "empty": {
          "text": "No plays in this period",
          "when": {
            "path": "plays",
            "equals": 0
          }
        },
        "map": {
          "points": "per_day",
          "labels": {
            "each": "days",
            "template": "{$|date}"
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Watching",
      "items": [
        {
          "key": "Watched",
          "value": "—"
        },
        {
          "key": "Last played",
          "value": "—"
        },
        {
          "key": "All time",
          "value": "—"
        },
        {
          "key": "Players",
          "value": "—"
        }
      ],
      "source": {
        "ref": "u",
        "map": {
          "items[0].value": {
            "if": {
              "path": "plays",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "if": {
                "path": "plays",
                "equals": 1
              },
              "then": {
                "template": "1 play · {watched|duration}"
              },
              "else": {
                "template": "{plays} plays · {watched|duration}"
              }
            }
          },
          "items[1].value": {
            "if": {
              "path": "history.response.data.data[0].date",
              "exists": true
            },
            "then": {
              "template": "{history.response.data.data[0].date|ago}"
            },
            "else": {
              "template": "Never"
            }
          },
          "items[2].value": {
            "if": {
              "path": "ever.total_plays",
              "exists": true
            },
            "then": {
              "if": {
                "path": "ever.total_plays",
                "equals": 1
              },
              "then": {
                "template": "1 play · {ever.total_time|duration}"
              },
              "else": {
                "template": "{ever.total_plays} plays · {ever.total_time|duration}"
              }
            },
            "else": {
              "template": "—"
            }
          },
          "items[3].value": {
            "if": {
              "path": "devices",
              "not": ""
            },
            "then": {
              "template": "{devices}"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "history",
      "type": "list",
      "label": "History",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "u",
        "empty": {
          "text": "Nothing watched yet",
          "when": {
            "value": {
              "count": "history.response.data.data",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "history.response.data.data",
            "limit": 7,
            "fields": {
              "key": {
                "template": "{row_id|raw}"
              },
              "text": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": {
                  "template": "{grandparent_title}"
                },
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": {
                    "template": "{parent_title}"
                  },
                  "else": {
                    "template": "{title}"
                  }
                }
              },
              "subtitle": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": {
                  "template": "S{parent_media_index|pad:2}E{media_index|pad:2} · {title}"
                },
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": {
                    "template": "{grandparent_title} · {title}"
                  },
                  "else": {
                    "if": {
                      "path": "percent_complete",
                      "lt": 85
                    },
                    "then": {
                      "if": {
                        "all": [
                          {
                            "path": "year",
                            "exists": true
                          },
                          {
                            "path": "year",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "template": "{year|raw} · stopped at {percent_complete}%"
                      },
                      "else": {
                        "template": "Stopped at {percent_complete}%"
                      }
                    },
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "year",
                            "exists": true
                          },
                          {
                            "path": "year",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "template": "{year|raw}"
                      },
                      "else": {
                        "template": ""
                      }
                    }
                  }
                }
              },
              "detail": {
                "template": "{date|ago}"
              },
              "url": {
                "template": "http://tautulli.local:8181/info?rating_key={rating_key|raw}"
              },
              "image": {
                "if": {
                  "all": [
                    {
                      "path": "thumb",
                      "exists": true
                    },
                    {
                      "path": "thumb",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "/api/v2?cmd=pms_image_proxy&img={thumb|raw}&width=300&height=450"
                },
                "else": {
                  "template": ""
                }
              },
              "image_shape": {
                "if": {
                  "path": "media_type",
                  "equals": "track"
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "icon": {
                "path": "media_type",
                "values": {
                  "movie": "film",
                  "episode": "tv",
                  "show": "tv",
                  "season": "tv",
                  "track": "music.note",
                  "album": "music.note",
                  "artist": "music.note",
                  "live": "antenna.radiowaves.left.and.right"
                },
                "default": "play.rectangle"
              }
            }
          }
        }
      }
    }
  ]
}
