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

{
  environment.systemPackages = [ pkgs.kanata ];

  # Add the Kanata service user to necessary groups
  systemd.services.kanata-razer.serviceConfig = {
    SupplementaryGroups = [
      "input"
      "uinput"
      "openrazer"
    ];
  };

  services.kanata = {
    enable = true;
    keyboards = {
      sinc =  {
        devices = [ "/dev/input/by-id/usb-Keebio_Sinc_Rev._4-event-kbd"
                    "/dev/input/by-id/usb-Keebio_Sinc_Rev._4-event-if02"
                    "/dev/input/by-id/usb-Keebio_Sinc_Rev._4-if02-event-joystick" ];
        config = ''
(defsrc
 esc  f1  f2  f3  f4  f5  f6  f7  f8  f9  f10  f11  f12  prtsc
 grv  1    2    3    4    5    6    7    8    9    0    -    =  \  del home
 tab  q    w    e    r    t    y    u    i    o    p    [    ]    bspc  end
 caps a    s    d    f    g    h    j    k    l    ;    '    ret  pgup
 lsft z    x    c    v    b    n    m    ,    .    /    rsft up pgdn
 lctl slck lmet lalt           spc            ralt rmet rctrl left down right)

(defalias
  supesc (tap-hold-press 100 200 esc lmet)
  sp-lsft (tap-hold-press 100 200 S-9 lsft)
  sp-rsft (tap-hold-press 100 200 S-0 lsft)
  controls (layer-while-held controls))

(deflayer qwerty
          esc  f1  f2  f3  f4  f5  f6  f7  f8  f9  f10  f11  f12  prtsc
          grv  1    2    3    4    5    6    7    8    9    0    -    = \ del home
          tab  q    w    e    r    t    y    u    i    o    p    [    ]  bspc end
          @supesc a    s    d    f    g    h    j    k    l    ;    '    ret pgup
          @sp-lsft z    x    c    v    b    n    m    ,    .    /    @sp-rsft up pgdn
          lmet @controls lalt lctrl          spc            rctrl ralt rmet left down right)

(deflayer controls
          esc  VolumeMute  VolumeDown  VolumeUp  f4  MediaTrackPrevious  MediaPlayPause MediaTrackNext f8  f9  f10  f11  f12  prtsc
          grv  1    2    3    4    5    6    7    8    9    0    -    = \ del home
          tab  q    w    e    r    t    y    u    i    o    p    [    ]  bspc end
          @supesc a    s    d    f    g    h    j    k    l    ;    '    ret pgup
          @sp-lsft z    x    c    v    b    n    m    ,    .    /    @sp-rsft pgup _
          lmet lmet lalt lctrl          spc            rctrl ralt rmet home pgdn end)

'';
      };
      razer = {
        devices = [ "/dev/input/by-id/usb-Razer_Razer_Blade-event-kbd"
                    "/dev/input/by-id/usb-Razer_Razer_Blade-if01-event-kbd" ];
        config = ''
(defsrc
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
  caps a    s    d    f    g    h    j    k    l    ;    '    ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctrl lmet lalt           spc            ralt rctrl)

(defalias
  supesc (tap-hold-press 100 200 esc lmet)
  sp-lsft (tap-hold-press 100 200 S-9 lsft)
  sp-rsft (tap-hold-press 100 200 S-0 lsft))

(deflayer qwerty
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
  @supesc a    s    d    f    g    h    j    k    l    ;    '    ret
  @sp-lsft z    x    c    v    b    n    m    ,    .    /    @sp-rsft
  lmet lalt lctrl           spc            rctrl ralt)
'';
      };
    };
  };
}
