# Last stable generation 359 10/13/24
# home-manager works by soft-linking the software packages configured by the user to /etc/profiles/per-user/your-username and modifying environment variables like PATH to point to this path, thus installing user software packages.
{
  inputs,
  outputs,
  lib,
  config,
  pkgs,
  systemSettings,
  userSettings,
  zen-browser,
  hyprland,
  nix-colors,
  ...
}:
let
  system = "x86_64-linux";

  myAliases = {
    # Sysadmin
    ll = "ls -alF";
    la = "ls -A";
    l = "ls -CF";
    ".." = "cd ..";
    version = "lsb_release -cs";
    cpu-speed = "cat /proc/cpuinfo | grep 'cpu MHz'";
    update = "sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y";
    nixFlakeUpdate = "nix flake update";
    listServices = "systemctl list-units --all --type=service --no-pager";
    listUserServices = "systemctl list-units --all --type=service --no-pager --user";
    listGenerations = "sudo nix-env -p /nix/var/nix/profiles/system --list-generations";
    switchGenerations = "nix-env --switch-generation $1";
    nixClean = "nix-env -e '*'";
    takeOutTrash = "nix-collect-garbage --delete-older-than $1";

    ls = "eza --icons -l -T -L=1";
    cat = "bat";
    htop = "btm";
    fd = "fd -Lu";
    w3m = "w3m -no-cookie -v";
    neofetch = "disfetch";
    fetch = "disfetch";

    # NixOS
    pkgsList="nix-env -qaP \\'*\\' --description";
    nixUpdate="nix-env -u \\'*\\'";
    updateChannel="nix-channel --update nixos";
    nixOptimize="nix-store --optimise";
    mkhome="cd ~/system && home-manager switch --flake .";
    buildsys="cd ~/system && sudo nixos-rebuild switch --flake \\'.\\#\\'";
    get256="nix-prefetch-url --type sha256 $1";

    remapKeys="input-remapper-control --command autoload";

    # Zoxide
    # cd="z";

    # Kitty
    icat="kitten icat";
    s="kitten ssh";
    kitty-logs="kitty --class='kitty-logs' journalctl -f";

    # init_0="sudo systemctl isolate emergency.target";
    # init_1="sudo systemctl isolate rescue.target";
    # init_3="sudo systemctl isolate multi-user.target";
    # init_5="sudo systemctl isolate graphical.target";

    stop-containers="listServices | grep container | grep active | awk '{print $1}' | xargs -n1 sudo systemctl stop";
    # history="history | fzf";
  };

  # URL of your dotfiles repository
  dotfilesRepo = "http://nas.local/git/wayne/dotfiles.git";

  # Path where the dotfiles repository will be cloned
  dotfilesPath = "${config.home.homeDirectory}/.dotfiles";

  # Path to the directory within the dotfiles repository that contains home directory dotfiles
  homeStowPath = "${dotfilesPath}/home";

  # pip hack
  # system = "x86_64-linux";
  # pkgs = pkgs.legacyPackages.${system};
  # buildPythonPackage = pkgs.python311Packages.buildPythonPackage;
  # fetchPypi = pkgs.python311Packages.fetchPypi;

  # Mopidy-WebSettings = buildPythonPackage rec {
  #   pname = "Mopidy-WebSettings";
  #   version = "0.2.3";
  #   format = "mopidyPlugin";

  #   src = fetchPypi {
  #     inherit pname version;
  #     sha256 = "6aa2284cd7af4a34cb10e60e721e92b98f06b2f064be2e1e2191f73374baa1ff";
  #   };

  #   doCheck = false;

  #   propogateBuildInputs = with pkgs.python311Packages; [];
  # };

  unstablePkgs = import (
    fetchTarball {
      url = "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz";
      sha256 = "146hn6w57hr341k5y003668gzh5l7shbikidn3r0ix33hb49sc5p";
    })
  {
    # config = config.nixpkgs.config;
    inherit system;
  };
in
{
  imports = [
    (import ./modules/hypr.nix { inherit pkgs config lib; })
    (import ./modules/theme.nix { inherit pkgs unstablePkgs; })
    ./modules/wayland.nix
    ./modules/xdg.nix

    # (import ../pkgs/app/audio/mopidy.nix { inherit pkgs; })
    # (import ../pkgs/app/editor/nvim.nix { pkgs = pkgs; })
    (import ../pkgs/git/git.nix { inherit userSettings pkgs; })
    (import ../pkgs/lang/cc/cc.nix { inherit pkgs; })
    # (import ../pkgs/lang/python/python.nix { pkgs = pkgs; })
    # (import ../pkgs/lang/python/python-packages.nix { pkgs = pkgs; })
    # (import ../pkgs/app/terminal/alacritty.nix { pkgs = pkgs; })
    (import ../pkgs/app/terminal/ghostty.nix { inherit pkgs; })
    (import ../pkgs/app/terminal/kitty.nix { inherit pkgs lib nix-colors config; })
    (import ../pkgs/app/terminal/parrot-starship.nix { inherit pkgs; })
    # (import ../pkgs/app/terminal/starship.nix { pkgs = pkgs; })
    (import ../pkgs/app/terminal/tmux.nix { inherit pkgs lib config; })
    (import ../pkgs/app/terminal/yazi.nix { inherit pkgs; })

    (import ../pkgs/shell/bash.nix { inherit pkgs config myAliases; })
    (import ../pkgs/shell/cli-collection.nix { inherit pkgs; })
    (import ../pkgs/shell/sh.nix { inherit myAliases pkgs;})
    (import ../pkgs/shell/stow.nix { inherit pkgs lib config; })
    (import ../pkgs/shell/zsh.nix { inherit myAliases  pkgs;})
  ];

  nixpkgs = {
    config = {
      allowUnfree = true;
    };
  };

  systemd.user.sessionVariables = config.home.sessionVariables;

  home = {
    username = "${userSettings.username}";
    homeDirectory = "/home/${userSettings.username}";
    sessionVariables = {
      EDITOR = userSettings.editor;
      TERM = userSettings.term;
      # PYTHONPATH = "${pkgs.mopidy}/lib/python3.11/site-packages:${pkgs.mopidy-tidal}/lib/python3.11/site-packages:${pkgs.mopidy-musicbox-webclient}/lib/python3.11/site-packages";
    };
  };
  fonts.fontconfig.enable = true;
  # colorScheme = nix-colors.colorSchemes.nord;

  home.packages = with pkgs; [
    zsh bash git stow tmux tmuxPlugins.tmux-fzf
    vim neovim lunarvim # vimPlugins.vim-kitty-navigator
    zoxide # vimPlugins.telescope-zoxide vimPlugins.zoxide-vim
    bottom htop jq
    starship
    yazi
    appimage-run
    nerdfonts ubuntu_font_family

    doublecmd filezilla
    libreoffice
    bottles winePackages.wayland

    vlc
  ] ++ (import ./pkgs { inherit unstablePkgs pkgs system zen-browser; });


  programs = {
    home-manager.enable = true;
    direnv = {
      enable = true;
      package = pkgs.direnv;
      enableBashIntegration = true;
      enableZshIntegration = true;
      nix-direnv = {
        enable = true;
        package = pkgs.nix-direnv;
      };
    };
    mpv = {
      enable = true;
      package = pkgs.mpv-unwrapped.wrapper { mpv = pkgs.mpv-unwrapped.override { vapoursynthSupport = true; }; youtubeSupport = true; };
      bindings = {
        WHEEL_UP = "seek 10";
        WHEEL_DOWN = "seek -10";
        "Alt+0" = "set window-scale 0.5";
      };
      # config = {

      # };
      # includes = [
      #   "${config.home.homeDirectory}/.config/mpv/config.inc"
      # ];
    };
    ripgrep.enable = true;
    vscode = {
      enable = true;
      extensions = with pkgs.vscode-extensions; [
        devsense.profiler-php-vscode
        devsense.phptools-vscode
        devsense.composer-php-vscode
        xdebug.php-debug
        firefox-devtools.vscode-firefox-debug
        bbenoist.nix
      ];
    };
  };
  services = {
    blueman-applet.enable = true;
    # keybase.enable = true;
    # kbfs = {
    #   enable = true;
    #   mountPoint = "keybase";
    #   extraFlags = [
    #     "-label: kbfs"
    #     "-mount-type: normal"
    #   ];
    # };
    nextcloud-client = {
      enable = false;
      startInBackground = false;
    };
    udiskie = {
      enable = true;
      notify = true;
      settings = {
        program_options = {
          tray = "auto";
          notify = true;
        };
        icon_names.media = [ "media-optical" ];
      };
    };
  };

  news.display = "silent";

  systemd.user.startServices = "sd-switch";

  home.stateVersion = "24.11";
}
