{
  config,
  lib,
  namespace,
  pkgs,
  ...
}:
let
  cfg = config.${namespace}.programs.waybar;

  waybar-media = pkgs.writeScriptBin "waybar-media" ''
    #!/usr/bin/env bash

    # Get current playing song from playerctl
    if command -v playerctl &> /dev/null; then
        # Check if any player is running
        if playerctl status &> /dev/null; then
            artist=$(playerctl metadata xesam:artist 2>/dev/null)
            title=$(playerctl metadata xesam:title 2>/dev/null)

            if [[ -n "$artist" && -n "$title" ]]; then
                echo "♪ $artist - $title"
            elif [[ -n "$title" ]]; then
                echo "♪ ''\${title//&/&amp;}"
            else
                echo "♪ Music Playing"
            fi
        else
            echo ""
        fi
    else
        echo ""
    fi
  '';

  waybar-media-art = pkgs.writeScriptBin "waybar-media-art" ''
    #!/usr/bin/env bash

    # Get current playing song from playerctl
    if command -v playerctl &> /dev/null; then
        # Check if any player is running
        if playerctl status &> /dev/null; then
            art=$(playerctl metadata mpris:artUrl 2>/dev/null)

            if [[ -n "$art" ]]; then
                echo ''\${art#file://}
            fi
        fi
    fi
  '';
in
{
  imports = [ ../options.nix ];

  config = lib.mkIf cfg.enable {
    home.packages = [
      waybar-media
      waybar-media-art
    ];
  };
}
