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

with lib;

let
  cfg = config.settings.applications.wezterm;
in {
  options = {
    settings.applications.wezterm.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Enable wezterm terminal
      '';
    };
  };

  config = mkIf cfg.enable {
    programs.wezterm = {
      enable = true;
      enableZshIntegration = true;
        package = pkgs.unstable.wezterm;
      extraConfig = /* lua */ ''
        -- Pull in the wezterm API
        local wezterm = require 'wezterm'

        -- This will hold the configuration.
        local config = wezterm.config_builder()
        local act = wezterm.action

        -- This is where you actually apply your config choices

        -- For example, changing the color scheme:
        config.enable_tab_bar = true
        config.use_fancy_tab_bar = false
        config.window_decorations = "NONE"
        config.tab_bar_at_bottom = true

        config.keys = {
          -- Pane controls
          {
            key = 'h',
            mods = 'CTRL',
            action = act.ActivatePaneDirection 'Left',
          },
          {
            key = 'l',
            mods = 'CTRL',
            action = act.ActivatePaneDirection 'Right',
          },
          {
            key = 'k',
            mods = 'CTRL',
            action = act.ActivatePaneDirection 'Up',
          },
          {
            key = 'j',
            mods = 'CTRL',
            action = act.ActivatePaneDirection 'Down',
          },

          -- Pane resizing
          {
            key = 'H',
            mods = 'CTRL',
            action = act.AdjustPaneSize { 'Left', 5 },
          },
          {
            key = 'L',
            mods = 'CTRL',
            action = act.AdjustPaneSize { 'Right', 5 },
          },
          {
            key = 'K',
            mods = 'CTRL',
            action = act.AdjustPaneSize { 'Up', 5 },
          },
          {
            key = 'J',
            mods = 'CTRL',
            action = act.AdjustPaneSize { 'Down', 5 },
          },
          {
            key = 'q',
            mods = 'CTRL',
            action = act.CloseCurrentPane { confirm = false },
          },

          -- Tab management
          { key = '1', mods = 'ALT', action = act.ActivateTab(0) },
          { key = '2', mods = 'ALT', action = act.ActivateTab(1) },
          { key = '3', mods = 'ALT', action = act.ActivateTab(2) },
          { key = '4', mods = 'ALT', action = act.ActivateTab(3) },
          { key = '5', mods = 'ALT', action = act.ActivateTab(4) },
          {
            key = 'q',
            mods = 'ALT',
            action = act.CloseCurrentTab { confirm = false },
          },
        }

        -- and finally, return the configuration to wezterm
        return config
      '';
    };
  };
}
