{ config, pkgs, ... }:
let
  beetsConfig = pkgs.writeText "beets-config.yaml" ''
    directory: ${config.home.homeDirectory}/Music
    library: ${config.home.homeDirectory}/.config/beets/library.db

    plugins:
      - fetchart
      - fish
      - musicbrainz
      - lyrics
      - web

    import:
      move: yes
      write: yes

    fetchart:
      auto: yes

    lyrics:
      auto: yes

    web:
      host: 127.0.0.1
      port: 8337
  '';
in
{
  home.packages = with pkgs; [
    beets
    fish

    # Common helpers used by beets/plugins
    ffmpeg
    imagemagick

    # Optional but often useful with beets:
    chromaprint
  ];

  # Install beets config into ~/.config/beets/config.yaml
  xdg.configFile."beets/config.yaml".source = beetsConfig;

  # Convenience: ensure beet uses that config without needing BEETSCONFIG each time
  home.sessionVariables = {
    BEETSCONFIG = "${config.xdg.configHome}/beets/config.yaml";
  };

  # Optional: a user service for `beet web`
  systemd.user.services.beets-web = {
    Unit = {
      Description = "Beets web plugin (user service)";
      After = [ "network.target" ];
    };
    Service = {
      ExecStart = "${pkgs.beets}/bin/beet web";
      Restart = "on-failure";
      Environment = [
        "BEETSCONFIG=${config.xdg.configHome}/beets/config.yaml"
      ];
    };
    Install = { WantedBy = [ "default.target" ]; };
  };
}

