{ config, lib, pkgs, inputs, ... }:

with lib;
let
  cfg = config.samfelag.modules.desktop.grapheio;
  hyprCfg = config.samfelag.modules.desktop.wm.hyprland;
in
{
  options.samfelag.modules.desktop.grapheio = {
    enable = mkEnableOption "Γραφείο desktop environment - hyprland based";
  };

  config = mkIf cfg.enable {

    # - Packages ---------------------------------

    environment.systemPackages = with pkgs; [
      # - Tools ----------------------------------
      jq
      # - Audio ----------------------------------
      pulseaudio
      # - Appearance -----------------------------
      rose-pine-gtk-theme
      rose-pine-icon-theme
      # - Cursor
      inputs.hyprcursor-rose-pine.packages.${pkgs.stdenv.hostPlatform.system}.default
      rose-pine-cursor
    ];

    # - Data files -------------------------------
    hm.xdg.configFile."grapheio".source = ../../config/grapheio;

    # - Cursor -----------------------------------
    hm.home.pointerCursor = {
      gtk.enable = true;
      package = pkgs.rose-pine-cursor;
      name = "BreezeX Cursor";
      size = 24;
    };

    hm.gtk = {
      enable = true;
      theme = {
        package = pkgs.rose-pine-gtk-theme;
        name = "rose-pine-gtk-theme";
      };
      iconTheme = {
        package = pkgs.rose-pine-icon-theme;
        name = "rose-pine-icons";
      };
      cursorTheme = {
        package = pkgs.rose-pine-cursor;
        name = "BreezeX-RoséPine";
        size = 24;
      };
    };

    # - Hyprland keybindings ---------------------
    hm.wayland.windowManager.hyprland.settings = lib.mkIf hyprCfg.enable {
        bind = [
          # Pulse Audio controls (mute)
          ", XF86AudioMute, exec, $HOME/.config/grapheio/scripts/volume.sh mute"

          # Media player controls
          ", XF86AudioPlay, exec, playerctl play-pause"
          ", XF86AudioPause, exec, playerctl play-pause"
          ", XF86AudioNext, exec, playerctl next"
          ", XF86AudioPrev, exec, playerctl previous"
          ", XF86AudioStop, exec, playerctl stop"

          # Keyboard brightness control
          "${hyprCfg.mod}, XF86MonBrightnessUp, exec, brightnessctl -d platform::kbd_backlight set +1"
          "${hyprCfg.mod}, XF86MonBrightnessDown, exec, brightnessctl -d platform::kbd_backlight set -1"
        ];

        binde = [
          # Pulse Audio controls (keep button pressed)
          ", XF86AudioRaiseVolume, exec, $HOME/.config/grapheio/scripts/volume.sh set +5%"
          ", XF86AudioLowerVolume, exec, $HOME/.config/grapheio/scripts/volume.sh set -5%"

          # Brightness control (keep button pressed)
          ", XF86MonBrightnessUp, exec, $HOME/.config/grapheio/scripts/brightness.sh set +5%"
          ", XF86MonBrightnessDown, exec, $HOME/.config/grapheio/scripts/brightness.sh set 5%-"

          # Keyboard brightness control (keep button pressed)
          "${hyprCfg.mod}, XF86MonBrightnessUp, exec, $HOME/.config/grapheio/scripts/brightness.sh kb_set +1"
          "${hyprCfg.mod}, XF86MonBrightnessDown, exec, $HOME/.config/grapheio/scripts/brightness.sh kb_set 1-"
        ];
    };

    # - Modules ----------------------------------

    samfelag.modules = {

      system.audio.enable = true;
      system.brightness.enable = true;

      desktop = {
        # Window Manager
        wm.hyprland.enable = true;

        tools = {
          # Greeter (login screen)
          greetd = {
            enable = true;
            wm_cmd = "Hyprland";
          };
          # Locker
          hyprlock.enable = true;
          # Status bars
          eww.enable = true;
          # Wallpapper
          hyprpaper.enable = true;
          # Launcher
          rofi.enable = true;
          # Notification center
          swaync.enable = true;
          # Clipboard manager (history)
          cliphist.enable = true;
          # Take screenshots
          screenshot.enable = true;
        };
      };

      app = {
        ghostty.enable = true;
        zen-browser.enable = true;
      };

    };
  };
}
